Groups | Blog | Home
all groups > asp.net building controls > april 2004 >

asp.net building controls : How do I save controls that are part of my custom control?


Ken Varn
4/15/2004 2:58:25 PM
I have a custom list control that allows other controls to be added to it
dynamically. I want to be able to maintain the custom list of controls on
postback. I tried storing each added control into a ViewState element, but
the WebControl object cannot be stored in ViewState (I get an exception
indicating some controls are not Serializable). This is a bit confusing,
because I can place these same controls in a form and their properties are
preserved (example: ListBox maintains the list of items in the list).

Can someone please indicate how to store the properties of controls that are
encapsulated in a custom control? I would have thought that a control would
expose a GetViewState() method or something like that, but it is protected.

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------

Jos
4/18/2004 8:52:29 PM
[quoted text, click to view]

You don't need to store the properties of controls. Controls store
their own properties in ViewState automatically. The only thing
you have to do is: to make sure that you recreate the same type
of controls after postback.

Just try to keep track of the number and type of controls that
you have. Use an array for instance, where you keep the
types (in the same order as on your control). Save this array
in ViewState.

On postback, read the array from Viewstate, and recreate
each control (without setting any properties). If this is done in
the right way, the controls will "magically" restore their own state.

--

Jos

Ken Varn
4/19/2004 10:22:43 AM
I should be able to store the number and type of each control, but how do I
know which ViewState items go with which control?

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------
[quoted text, click to view]

AddThis Social Bookmark Button