Gridview sorting and paging with dynamically generated controls

I hit the brine using dynamic controls in the grid.

I bind gridview to the list and then add dyanmic controls. In order to maintain control state, I need to do this when the page loads. However, since events fire after the page loads, I cannot correctly handle sorting and swap events. Are there any creative solutions for this problem, or am I not mistaken about this?

EDIT: I'm not sure I correctly explained my problem. It doesn't matter where I add dynamic controls in the life cycle. The problem is that the sort and swap events require me to re-bind the GridView, which (apparently) causes the properties assigned to my dynamic controls to get lost from the ViewState, since the sort and search call bind occurs at the end of the life cycle.

protected void Page_Load(object sender, EventArgs e)
{       
   //NOTE: to maintain control state of dynamic controls all databinding needs to be done in page load. 
   grdProducts.DataSource = GetDataSource();
   grdProducts.DataBind();
}

protected void grdProducts_OnRowDatabound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType != DataControlRowType.DataRow) return;        

    ManufacturerProduct m = new ManufacturerProduct();

    m.Model = DataBinder.Eval(e.Row.DataItem, "Model").ToString();

    PlaceHolder ph = new PlaceHolder();        
    ph = (PlaceHolder)e.Row.FindControl("phAddToCart");        

    LinkButton lb = new LinkButton();
    lb.Text = "Add To Cart";
    //NOTE: if I bind after page load the command never fires.      
    lb.Command +=  new CommandEventHandler(AddItem);
    lb.CommandName = "AddItem";
    lb.CommandArgument = m.Model;        
    ph.Controls.Add(lb);

}

protected void grdProducts_OnSorting(object sender, GridViewSortEventArgs e)
{
    //NOTE: since events fire after page load I can't handle this properly.

    hfSortExpression.Value = e.SortExpression.ToString();

    grdProducts.PageIndex = 0;
    //NOTE: If I rebind here I hose my dynamic controls
    //grdProducts.DataSource = GetDataSource();
    //grdProducts.DataBind();
}
+3
source share
4 answers

Well, the answer for me was to rethink how I attacked the problem. I got rid of the dynamic linkbutton and added a button field instead. That way, I could snap a gridview where ever and its control state didn't matter. Here is the page that helped me.

http://msdn.microsoft.com/en-us/library/bb907626.aspx

+1

PreRender
ASP.NET
PreRender , , , , .

0

OnRowCreated GridView , . OnRowCreated , , DataBind() .

OnRowDatabound .

.

0

, Page_Load():

grdProducts.DataSource = GetDataSource();
grdProducts.DataBind();

When I dinamicaly bind a datasource to my control, I have to redefine OnInit like this and take two lines and put them there:

protected override void OnInit(EventArgs e) {
   grdProducts.DataSource = GetDataSource();
   grdProducts.DataBind();
   base.OnInit(e);
}

It then maintains the state since it is prior to initializing the ViewState page. As Thomas said, take a look at the links he gave you.

Note: Sorry for my English, I am from Quebec and usually speak French.

0
source

All Articles