[C#.NET 2.0] ViewState and EventHandlers

G

Guest

Hi!

I have an application that looks something like this:
************************************************************
Order.aspx
|--Thanks.ascx
|--Step.ascx
|--MultiView
|--(View1)Whois.ascx (contains EventHandler's)
|--(View2)Webbpaket.ascx
|--(View3)ContactInfo1.ascx
|--(View3)Login.ascx (contains EventHandler's)
|--etc...
|--Start.ascx
************************************************************

The "user controls" is shown/hided (visible=true/false) in the MultiView
depending on the querystring and some other info, ex: if the querystring is
"?Action=1" all UC's will be used in the Multiview but if the querystring is
"?Action=2" then all except "View2" will be used in the Multiview and it's
here where my problems start.

When "View2" is hidden my UC's containing an eventhandler will cause the
error:

**Error******************************************************
Failed to load viewstate. The control tree into which viewstate is being
loaded must match the control tree that was used to save viewstate during the
previous request. For example, when adding controls dynamically, the controls
added during a post-back must match the type and position of the controls
added during the initial request.
************************************************************

I assume that it is the viewstate in Order.aspx that complains and not in
one of my UC's, but how do I solve this? Setting "EnableViewstate=false" is
out of the question!
 
G

Guest

As the error message says, to keep the ViewState instact you have to
recreate all user controls, even if they are not going to be visible.
 
G

Guest

Hi!

Thanks, but I'm doing that, the problem occurs only when I use an
eventhandler in the usercontrol.

Regards
Anders Aleborg
Aleborg Solutions AB
 
G

Guest

I see. I assume that you recreate the user controls in the Page_Load
event? Then the problem is that the event from the user control occures
before Page_Load. You have to create the user controls in a page event
that occurs earlier, like PreInit or InitComplete.
 

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

Top