How do I save controls that are part of my custom control?

K

Ken Varn

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
-----------------------------------
 
J

Jos

Ken said:
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.

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.
 
K

Ken Varn

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
-----------------------------------
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top