UserControl and ViewState Error

  • Thread starter Thread starter Tim
  • Start date Start date
T

Tim

Greetings!

I have a UserControl. On this UserControl is a Panel and a
RadioButtonList. The Panel's Visible property is set to false by
default. When the user selects a particular RadioButtonList value, a
postback is performed and the Panel's Visible property is set to true.
Selecting a different RadioButtonList value sets the Visible property
to false again.

So far, so good. It does, in fact, do this.

The UserControl is one of many that are loaded into a Page. I load all
of the UserControls in using LoadControl during the Page's Init event
(as I'm told I must do by several other articles posted to Usenet). On
the Page is a PlaceHolder and a DropDownList. Depending on the
selection of the DropDownList, the appropriate UserControl is added to
the Placeholder's Control collection, thus making that UserControl
visible to the user.

Switching between the UserControl's also works.

The problem I'm having arises when the selection of the DropDownList
displays the aforementioned UserControl (the one with the
RadioButtonList and Panel) and the user selects the RadioButtonList
option that make the hidden Panel visible. If the user then changes
which UserControl is to be visible via the DropDownList, I receive the
following 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.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: 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.

And the stack trace...

I know why the error occurs. What I need to know is how do I prevent
it from occurring. Would any of you fine Usenet folks be able to
assist me?

I'm afraid I'm not too familiar with ViewState and any tutorials,
guides, and other forms of assistance that say it is easy to use
completely fail to describe it adequately. Please keep this in mind
before posting back that "It's easy. All you have to do is change the
ViewState to...".

Thanks.
 
Tim said:
I'm afraid I'm not too familiar with ViewState and any tutorials,
guides, and other forms of assistance that say it is easy to use
completely fail to describe it adequately. Please keep this in mind
before posting back that "It's easy. All you have to do is change the
ViewState to...".

A dropdown does not persist its value via Viewstate. It uses the
IPostBackDataHandler interface. Therefore, you should investigate whether
you even need Viewstate at all.
 
Back
Top