By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 772 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

Help with Loading ASCX control during PostBack

P: n/a
I've got a page which loads up a different user control into a placeholder
control every time a button is clicked on the parent page. I use a
statement like Me.plcTabViews.Controls.Add(LoadControl("test.ascx ")). There
are up to 30 possibilities which are decided by CASE statements and keeps
one from loading up onto another. However, I'm running into a unique
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.

This leads me to believe that in my code, I've not done something correctly.
It works fine loading up controls when I click or load several of the pages,
but it may fail after a few parent button/option clicks. Please advice!

TIA
John Cosmas
Nov 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
John,
Your code is fine. There are a couple ways to deal with the problem. But
first a brief explanation. Basically when you add a control to the page,
the controls within it (grids, dropdownlists...) will store their values in
the viewstate. On postback, it goes through the viewstate and reassigns the
values stored there into the controls. If your controls don't match up, you
get the error you got. Example:

1st click --> Load test.ascx -->
Me.plcTabViews.Controls.Add(LoadControl("test.ascx ")) --> test.ascx loads
whatever it needs to into the viewstate (as the first child of plcTabViews)
2nd click --> Load test2.ascx -->
Me.plcTabViews.Controls.Add(LoadControl("test2.asc x")) --> grabs the
viewstate for the first child on plcTabViews and tries to load it into
test2.ascx --> CRASH

Your solutions are:
- Disable viewstate on all your controls. You can do this by putting
EnableViewState="False" in the @Control directive of each control. If those
controls need viewstate, they won't behave as expected.

- Reload the previous control and then clear it, then load the control you
want. Using the above example, the 2nd click would look like this:
2nd click --> Load test.ascx --> it grabs the viewstate --> Remove
test.ascx --> Load test2.ascx --> ...
This will give you extra overhead but can fairly easily be done

- Instead of using postback to load controls, don't postback and use
querystring values.

Considering you aren't reloading the controls, my guess is you don't need
anything from their viewstate and the 1st option will be your best bet.

Karl
"John Cosmas" <va*****@msn.com> wrote in message
news:uX**************@TK2MSFTNGP12.phx.gbl...
I've got a page which loads up a different user control into a placeholder
control every time a button is clicked on the parent page. I use a
statement like Me.plcTabViews.Controls.Add(LoadControl("test.ascx ")). There are up to 30 possibilities which are decided by CASE statements and keeps
one from loading up onto another. However, I'm running into a unique
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.

This leads me to believe that in my code, I've not done something correctly. It works fine loading up controls when I click or load several of the pages, but it may fail after a few parent button/option clicks. Please advice!

TIA
John Cosmas

Nov 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.