Persisting dynamic GridView across postbacks

Last post 04-24-2008 7:55 AM by Filip123. 3 replies.

Sort Posts:

  • Persisting dynamic GridView across postbacks

    03-05-2008, 4:39 PM
    • Loading...
    • Doctor Sid
    • Joined on 03-05-2008, 3:44 PM
    • Posts 14

    I'm dynamically adding BoundFields and TemplateFields to a GridView. This involves expensive (database-)operations, so I can't do it on each postback. Is there a possible way to persist the final state across postbacks?

    I always run into errors on restoring the GridView object from the session or cache. I already did this for simple dynamic controls, but it doesn't seem to work for the GridView as soon as it involves dynamically added columns.

  • Re: Persisting dynamic GridView across postbacks

    03-05-2008, 10:25 PM
    • Loading...
    • Ricardojb
    • Joined on 01-27-2005, 9:23 PM
    • Posts 115

    If you create the grid dynamically, you have to recreate it every time unless you create it in the PreInit event (which is unlikely). The control's tree viewstate is loaded before the OnLoad event so it can't recreate dynamically created controls, however, the form will persist the ViewState of the grid.

     I wouldn't store the gridview in session or cache, I woulr rather store the datasource there (whatever it is) and rebind the grid.

  • Re: Persisting dynamic GridView across postbacks

    03-06-2008, 8:28 AM
    Answer
    • Loading...
    • Doctor Sid
    • Joined on 03-05-2008, 3:44 PM
    • Posts 14

    I finally figured out an acceptable solution.
    The TemplateFields themselves are automatically restored from viewstate, but the templates themselves aren't (the field's Item/Header/FooterTemplate properties are null).

    Note: I can't explain it, but I have to call gv.Columns.Clear() before adding dynamic columns, otherwise it doesn't even remember the TemplateFields...

    So my solution is to cache the gridview's columns collection and call the following method in Page_Load (or earlier): 

        protected void restore_columns() {
    DataControlFieldCollection columns = (DataControlFieldCollection)Cache["cols"];
    gvSchluessel.Columns.Clear(); // gvSchluessel is the GridView
    foreach (DataControlField field in columns) {
    gvSchluessel.Columns.Add(field);
    if (field.GetType() == typeof(TemplateField)) {
    TemplateField tf = (TemplateField)field;
    int i = gvSchluessel.Columns.IndexOf(tf);
    if (tf.HeaderTemplate != null) {
    tf.HeaderTemplate.InstantiateIn(gvSchluessel.HeaderRow.Cells[i]);
    }
    foreach (GridViewRow row in gvSchluessel.Rows)
    {
    if (tf.ItemTemplate != null) {
    tf.ItemTemplate.InstantiateIn(row.Cells[i]);
    }
    }
    if (tf.FooterTemplate != null) {
    tf.FooterTemplate.InstantiateIn(gvSchluessel.FooterRow.Cells[i]);
    }
    }
    }
    }

    Note that I call the template's InstantiateIn() method manually instead of binding the gridview, so I don't need to cache any data source for it.
    If your grid also contains many predefined fields (bound fields etc.), you could further improve performance by only caching and replacing the TemplateFields.

    I hope this is of use for some of you.

  • Re: Persisting dynamic GridView across postbacks

    04-24-2008, 7:55 AM
    • Loading...
    • Filip123
    • Joined on 08-16-2006, 6:30 AM
    • Posts 26

    I try this Solution and is still not working. Where do you make the cache of columns. Could you post the entire solution.

    Thanks

Page 1 of 1 (4 items)
Microsoft Communities
Page view counter