CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Peter's Gekko

public Blog MyNotepad : Imho { }

Adding a control a runtime : what is not in the viewstate

In several earlier post on adding controls at runtime and the potential impact on the viewstate I could have created some confusion. You can add controls at runtime but these controls will not become part of the viewstate of their parent control (the page itself is also a control). If you want to persist the new control over  roundtrips you have to add the control again and again on every roundtrip. The viewstate is used to store all those properties which pop up in the property window.

Instead of adding a control again and again I personally prefer to add the control at design time and set its visible property to false. When the control is needed it will become available by setting the property programmtically. An even nicer way is to bind the visible property to a custom expression, like the name of a (protected) method on the page.

Peter



Comments

Peter van Ooijen said:

Responses on earlier posts indicate that some people expect the control to be re-created because it is in the viewstate. That's the myth I wanted to get out of the way.

There's a good chance it will participate in the viewstate if you recreate it before the page starts tracking viewstate. That's in the init. I agree on that.

# March 30, 2004 7:09 AM

noob said:

and how do you do that?
# July 6, 2006 2:42 PM

pvanooijen said:

The link is updated and works again. Just follow that for a code example.
# July 6, 2006 4:36 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!

This Blog

Syndication

News