Home All Groups Group Topic Archive Search About

DataGrid rows not recreated from ViewState

Author
13 Sep 2006 8:22 PM
dc
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

Author
14 Sep 2006 7:39 AM
dc
A new day, a new idea. I found a similar problem in another post:

http://groups.google.de/group/microsoft.public.dotnet.framework.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

d*@upsize.de wrote:
Show quoteHide quote
> 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