Discussion:
programmatically adding controls and ViewState
Smotritsky, Alex
2007-07-06 20:38:50 UTC
Permalink
This Microsoft page:



http://msdn2.microsoft.com/en-us/library/hbdfdyh7(VS.80).aspx



says:



If you insert dynamic controls between existing controls, the dynamic
control's view state information is inserted into the corresponding
location of the view state structure. When the page is posted and the
view state is loaded, the dynamic control does not yet exist; therefore,
the extra information in view state does not correspond to the right
control. The result is usually an error indicating an invalid cast.

A control I'm dynamically inserting is disappearing on postback. I think
that's because of the above mentioned issue. Is there a solution to
this?


===================================
This list is hosted by DevelopMentor� http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
Adam Sills
2007-07-06 20:55:36 UTC
Permalink
The actual composition of a page during every execution is loaded from the
ASPX contents. Anything you add dynamically in your codebehind must be
repeated every time the page loads.

If you want your dynamically added controls to have usable ViewState, you
have to add them to the page before the Load event (technically before
ViewState is loaded). An easy place to put it is inside Init.

Adam..


-----Original Message-----
From: Discussion of building .NET applications targeted for the Web
[mailto:DOTNET-***@DISCUSS.DEVELOP.COM] On Behalf Of Smotritsky, Alex
Sent: Friday, July 06, 2007 3:39 PM
To: DOTNET-***@DISCUSS.DEVELOP.COM
Subject: [DOTNET-WEB] programmatically adding controls and ViewState

This Microsoft page:



http://msdn2.microsoft.com/en-us/library/hbdfdyh7(VS.80).aspx



says:



If you insert dynamic controls between existing controls, the dynamic
control's view state information is inserted into the corresponding
location of the view state structure. When the page is posted and the
view state is loaded, the dynamic control does not yet exist; therefore,
the extra information in view state does not correspond to the right
control. The result is usually an error indicating an invalid cast.

A control I'm dynamically inserting is disappearing on postback. I think
that's because of the above mentioned issue. Is there a solution to
this?


===================================
This list is hosted by DevelopMentor. http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

===================================
This list is hosted by DevelopMentor� http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
Loading...