locked
Losing TemplateField Controls in Gridview After Postback RRS feed

  • Question

  • I saw a few older threads on this, but no real answers.

    I am building a nested Gridview.  I have to add a child Gridview to each row of the parent dynamically.  Both the layout and content of the child Gridviews are based on the data from the parent row.  For example, one child Gridview may have 5 columns, another 6, another 4, and some no child gridview at all.

    I am dynamically creating the child Gridviews in the RowDataBound event of each parent row, adding controls to TemplateFields.  The page loads great the first time, but when I do a PostBack, the child Gridviews still have the correct row and column counts, but no data/controls.  I'm assuming the ViewState remembers the data since, the row and column counts are correct, but perhaps the controls in TemplateFields are lost.

    Any ideas?

    Thanks

    • Moved by Mike Feng Tuesday, May 22, 2012 7:50 AM Asp.net (From:.NET Base Class Library)
    Monday, May 21, 2012 12:37 PM

Answers

    1. This is not the correct forum.  The correct one is http://forums.asp.net.
    2. The problem comes from the fact that you are not complying with the golden rule:  Dynamic controls must be recreated every time the page posts.  Since the ViewState of the parent GridView remembers everything, the RowDataBound event is not fired (not needed) on post back.  This means that your controls in the template fields of the child grid view(s) are not created on post back.  Find a way to ensure the controls are always re-created.  For example, you might want to process Page_PreRender and in there make sure that on post back all pertinent controls are present; if not, re-create them.

    Jose R. MCP

    • Proposed as answer by Mike Feng Tuesday, May 22, 2012 7:50 AM
    • Marked as answer by Mr. Wharty Thursday, May 31, 2012 12:03 AM
    Monday, May 21, 2012 1:25 PM

All replies

    1. This is not the correct forum.  The correct one is http://forums.asp.net.
    2. The problem comes from the fact that you are not complying with the golden rule:  Dynamic controls must be recreated every time the page posts.  Since the ViewState of the parent GridView remembers everything, the RowDataBound event is not fired (not needed) on post back.  This means that your controls in the template fields of the child grid view(s) are not created on post back.  Find a way to ensure the controls are always re-created.  For example, you might want to process Page_PreRender and in there make sure that on post back all pertinent controls are present; if not, re-create them.

    Jose R. MCP

    • Proposed as answer by Mike Feng Tuesday, May 22, 2012 7:50 AM
    • Marked as answer by Mr. Wharty Thursday, May 31, 2012 12:03 AM
    Monday, May 21, 2012 1:25 PM
  • Yeah, the kicker is I can't create the child grids until I know the data in the parrent row, plus some checkboxes may have been checked off, so have to remember those too.

    Anyway, I tried http://forums.asp.net first, but had no Start New Thread button, and I am logged in, but I will look into that and move this post.

    Thanks

    Monday, May 21, 2012 2:13 PM