DataGrid rows not recreated from ViewState

D

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
 
D

dc

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top