DataGrid rows not recreated from ViewState

Discussion in 'ASP .Net Web Controls' started by dc@upsize.de, Sep 13, 2006.

  1. Guest

    Hi,

    I developed a Usercontrol that primarily contains a datagrid. If the
    selected item of the datagrid changes, the rows would be recreated from
    viewstate.

    Everything works fine on a simple test page. But in our production
    environment the control will be loaded via Page.LoadControl and be
    nested inside a more delicate control tree. And that's the problem: in
    this environment the DataGrid simply ceases to exist on a postback.

    Our BuildControls() routine sits in "OnInit" of default.aspx. When I
    simply move the BuildControls() call to Page_Load, then my control
    works fine. But then many other of our controls start to behave freaky,
    postbacks stop working. So that is not an option.

    So what can I do to circumvent the problem in my usercontrol (since I
    cannot rewrite all the other controls)? I thought the reason is, that
    the ViewState must be loaded before the DataGrid starts creating it's
    child controls. But when I try to isolate the problem and dynamically
    load the control in the OnInit method of a simple project, everything
    works fine, too.

    I compared the ViewState of the Page_Load / OnInit versions:
    identitcal. I also compared the sequence of method calls of the test
    project and the production project. Seems identical, too. When I call
    BuildControls() from OnInit, both projects execute these methods in the
    following order:

    Default.aspx.OnInit
    UserControl.CreateControlCollection
    UserControl.OnInit
    Default.aspx.LoadViewState
    UserControl.LoadViewState
    Default.aspx.Page_Load
    UserControl.Page_Load

    I thought the problem must be that the ViewState is loaded after
    UserControl.CreateControlCollection, but that's not it since the same
    call order is found in the working project.

    I am really stumped and very happy about any hint on this.

    Regards
    DC
     
    , Sep 13, 2006
    #1
    1. Advertising

  2. Guest

    A new day, a new idea. I found a similar problem in another post:

    http://groups.google.de/group/micro...ework.aspnet.webcontrols/msg/a8a8f85ff9eb7ff0

    Basically, by deriving from DataGrid and adding only

    protected override void LoadViewState(object savedState)
    {
    base.LoadViewState (savedState);
    if (ChildControlsCreated)
    CreateControlHierarchy(false);
    }

    I am able to circumvent the problem, I did not find adverse side
    effects as of now. Appears awkward to me, though.

    Maybe somebody knows a better solution or at least an explanation. I
    forgot to mention that this is all about ASP.Net 1.1.

    Regards
    DC

    wrote:
    > Hi,
    >
    > I developed a Usercontrol that primarily contains a datagrid. If the
    > selected item of the datagrid changes, the rows would be recreated from
    > viewstate.
    >
    > Everything works fine on a simple test page. But in our production
    > environment the control will be loaded via Page.LoadControl and be
    > nested inside a more delicate control tree. And that's the problem: in
    > this environment the DataGrid simply ceases to exist on a postback.
    >
    > Our BuildControls() routine sits in "OnInit" of default.aspx. When I
    > simply move the BuildControls() call to Page_Load, then my control
    > works fine. But then many other of our controls start to behave freaky,
    > postbacks stop working. So that is not an option.
    >
    > So what can I do to circumvent the problem in my usercontrol (since I
    > cannot rewrite all the other controls)? I thought the reason is, that
    > the ViewState must be loaded before the DataGrid starts creating it's
    > child controls. But when I try to isolate the problem and dynamically
    > load the control in the OnInit method of a simple project, everything
    > works fine, too.
    >
    > I compared the ViewState of the Page_Load / OnInit versions:
    > identitcal. I also compared the sequence of method calls of the test
    > project and the production project. Seems identical, too. When I call
    > BuildControls() from OnInit, both projects execute these methods in the
    > following order:
    >
    > Default.aspx.OnInit
    > UserControl.CreateControlCollection
    > UserControl.OnInit
    > Default.aspx.LoadViewState
    > UserControl.LoadViewState
    > Default.aspx.Page_Load
    > UserControl.Page_Load
    >
    > I thought the problem must be that the ViewState is loaded after
    > UserControl.CreateControlCollection, but that's not it since the same
    > call order is found in the working project.
    >
    > I am really stumped and very happy about any hint on this.
    >
    > Regards
    > DC
     
    , Sep 14, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. McGeeky
    Replies:
    2
    Views:
    425
    McGeeky
    Dec 1, 2005
  2. Anonymous
    Replies:
    1
    Views:
    382
    Andrew Thompson
    Nov 27, 2004
  3. Andy B
    Replies:
    1
    Views:
    328
    gerry
    May 23, 2008
  4. Earl Teigrob

    Dynamically Recreated Child Controls not Retaining View State

    Earl Teigrob, Oct 6, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    137
    Earl Teigrob
    Oct 6, 2004
  5. Allan Ebdrup

    Adding rows to a Tablecontrol does not save in viewstate

    Allan Ebdrup, Oct 3, 2005, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    169
    Allan Ebdrup
    Oct 5, 2005
Loading...

Share This Page