Steve D

Steve D

  • NA
  • 3
  • 0

Gridview powered website paging function doesn't function with inserted gridview row

Mar 11 2010 1:55 AM
Hi,

     My team and I are currently working on a project which should function like this:
       - The index.aspx page has a gridview which retrieves images stored as binary data from a database
       - We have paging enabled so that we can go page by page and look at all the entries in the database
       - At the onrowcreated event a method in the code behind counts 9 rows in the gridview and inserts an adrotator

The problems is that if we have if(!IsPostBack) inside the RowCreated Method, the first page of the gridview will have the adrotator but the second page will not display and we get an error which is as follows:

Failed to load viewstate.  The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request.

AND

If we have if(IsPostBack) the first page of the gridview will not have an adrotator on the first page but the second page will have the adrotator, and if we click on the previous page we get the same error.

AND

If we leave out the postback the same problem as with if(!IsPostBack) happens

Here is the code that we have for the index.aspx.cs page:

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class index : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs a)
    {
       
    }

    //protected void lblOne_Click(object sender, EventArgs e)
    //{
        //lblTag.Text = btnOne.Text;
    //}

    protected void RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (!IsPostBack)
            if (e.Row.RowIndex == 8)
            {
                Table tbl = e.Row.Parent as Table;
                if (tbl != null)
                {
                    GridViewRow row = new GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal);
                    TableCell cell = new TableCell();

                    cell.ColumnSpan = 4;

                    AdRotator gvAd = new AdRotator();
                    gvAd.DataSourceID = "SqlDataSource3";
                    gvAd.ImageUrlField = "Image_Location";
                    gvAd.NavigateUrlField = "Image_Url";

                    cell.Controls.AddAt(0, gvAd);
                    row.Cells.Add(cell);
                    tbl.Rows.AddAt(9, row);
                }
            }
    }
}





Answers (2)