Monday, March 12, 2012

Why ViewState/ControlState Loaded After CreateChildControls?

Tonight's episode of me beating my head bloody against the monitor was the result of ViewState/ControlState information not being
available when CreateChildControls() gets called for a CompositeControl.

So, my question is, >>why<< isn't ViewState/ControlState available before CreateChildControls() gets called by the Framework? Is it
just me, or does it seem really counterintuitive to be creating child controls on postback without the previously-stored state
information being available? Is it that ViewState/ControlState are associated with the controls themselves, so before the controls
are created the state data can't be made available?

- MarkAre you dynamically adding the composite control to the page?

--
~~~~~~~~~~~
Ben Rush
http://www.ben-rush.net/compilererrors
"Mark Olbert" <ChairmanMAO@.newsgroups.nospamwrote in message
news:vne0s25h7t26brb6qo4slbc8iqi47civm6@.4ax.com...

Quote:

Originally Posted by

Tonight's episode of me beating my head bloody against the monitor was the
result of ViewState/ControlState information not being
available when CreateChildControls() gets called for a CompositeControl.
>
So, my question is, >>why<< isn't ViewState/ControlState available before
CreateChildControls() gets called by the Framework? Is it
just me, or does it seem really counterintuitive to be creating child
controls on postback without the previously-stored state
information being available? Is it that ViewState/ControlState are
associated with the controls themselves, so before the controls
are created the state data can't be made available?
>
- Mark


Mark,

I think it makes perfect sense. ViewState contains values that are to be
assigned to control properties. Obviously, the controls are supposed to be
created before this. Read more about the page life cycle:

http://msdn2.microsoft.com/en-us/li...472(VS.80).aspx
--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
"Mark Olbert" <ChairmanMAO@.newsgroups.nospamwrote in message
news:vne0s25h7t26brb6qo4slbc8iqi47civm6@.4ax.com...

Quote:

Originally Posted by

Tonight's episode of me beating my head bloody against the monitor was the
result of ViewState/ControlState information not being
available when CreateChildControls() gets called for a CompositeControl.
>
So, my question is, >>why<< isn't ViewState/ControlState available before
CreateChildControls() gets called by the Framework? Is it
just me, or does it seem really counterintuitive to be creating child
controls on postback without the previously-stored state
information being available? Is it that ViewState/ControlState are
associated with the controls themselves, so before the controls
are created the state data can't be made available?
>
- Mark


Thanks for Eliyahu's input,

Hi Mark,

As Eliyahu has mentioned, the restore of controlstate/viewstate does occur
after the control's "CreatechildControls" be called. Actually, except the
first request(before postback), in each postback request, the control will
first reconstruct its control Tree by calling "CreateChildControls" method.
Then, it perform Init on the control hierarchy and restore
viewstate/control state from the persistent storage(the <input hidden >
element in page). And In Load event, the ViewState should has been
available there. So generally, in "CreateChildControls" method, we should
only put code that simply construct the control tree of webcontrol, and the
code logic there should not rely on those resource that are not available
at that time(such as ViewState).

I think you may have some code logic about the control structure(sub
control) depend on the viewstate. Would you provide some further
information on this? We can try looking for some other approaches to
overcome the issue.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscript...ault.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscript...rt/default.aspx.

==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Me, I tend to like to have all my information and "stuff" available before building things...whether it's a woodworking project,
cooking a meal, or creating custom controls :). Just a personal quirk, I guess, and it's why I find it so counter-intuitive that
ViewState/ControlState info isn't available until after I build a constituent control.

Thanks for the reference.

- Mark

On Wed, 31 Jan 2007 10:18:42 +0200, "Eliyahu Goldin" <REMOVEALLCAPITALSeEgGoldDinN@.mMvVpPsS.orgwrote:

Quote:

Originally Posted by

>Mark,
>
>I think it makes perfect sense. ViewState contains values that are to be
>assigned to control properties. Obviously, the controls are supposed to be
>created before this. Read more about the page life cycle:
>
>http://msdn2.microsoft.com/en-us/li...472(VS.80).aspx


I agree that it's necessary to have all the information and stuff available
before building since that can avoid running into a dead way. Also,
sometimes those programming model like the ASP.NET control lifecycle look
like paradox, however, after we inspect it detailedly, we would find it
quite straighforward to use. Anyway, please feel free to post here if you
meet any other problems or have any questions on this.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

This posting is provided "AS IS" with no warranties, and confers no rights.

0 comments:

Post a Comment