Maintaining state for a programatically loaded control

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I've written several UserControls that contain different versions of a form
that users need to fill out. Depending on which role a user is logged in as,
I load the appropriate UserControl and add it to a panel on my WebForm. So
far, so good. However, when the form is posted back to the server, I'm
getting stuck.

I don't think I want to load the control into the panel again because then
all of the values that were set by the user before posting back will be lost.
Still, I have to get an instance of the control somehow.

I thought maybe I'd stuff the entire UserControl into the ViewState (the
user controls are not very big), but then I get an error message saying, "The
type 'ASP.MyDataEntryForm_ascx' must be marked as Serializable or have a
TypeConverter other than ReferenceConverter to be put in viewstate." I've put
a [Serializable] attribute on the control's class, but it doesn't seem to
help. The control only contains other WebControls, all of which can have
ViewState enabled.

Any ideas, or should I go for a completely different approach like
redirecting to different pages instead of loading different controls into a
single page? Thanks!
 
Hi P:

Actually: you DO want to reload the user control on each postback
during the Load event. The controls will pick up the values correctly
from the ViewState and Form elements.

HTH,
 
Thanks, HTH. That worked, of course.

It just seems counter-intuitive to me that you would create a new instance
of the dynamically created control and add it to the page every time the page
is hit. I thought this would've caused the control values set by the user to
be lost. I was wrong.

Scott Allen said:
Hi P:

Actually: you DO want to reload the user control on each postback
during the Load event. The controls will pick up the values correctly
from the ViewState and Form elements.

HTH,
--
Scott
http://www.OdeToCode.com/blogs/scott/

I've written several UserControls that contain different versions of a form
that users need to fill out. Depending on which role a user is logged in as,
I load the appropriate UserControl and add it to a panel on my WebForm. So
far, so good. However, when the form is posted back to the server, I'm
getting stuck.

I don't think I want to load the control into the panel again because then
all of the values that were set by the user before posting back will be lost.
Still, I have to get an instance of the control somehow.

I thought maybe I'd stuff the entire UserControl into the ViewState (the
user controls are not very big), but then I get an error message saying, "The
type 'ASP.MyDataEntryForm_ascx' must be marked as Serializable or have a
TypeConverter other than ReferenceConverter to be put in viewstate." I've put
a [Serializable] attribute on the control's class, but it doesn't seem to
help. The control only contains other WebControls, all of which can have
ViewState enabled.

Any ideas, or should I go for a completely different approach like
redirecting to different pages instead of loading different controls into a
single page? Thanks!
 
Back
Top