Help with Loading ASCX control during PostBack


J

John Cosmas

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
 
Ad

Advertisements

K

Karl

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.ascx")) --> 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
 
Joined
Mar 26, 2007
Messages
1
Reaction score
0
awesome.

this is exactly my problem and your ideas have opened my eyes to the solution. thanks so much.
 
Ad

Advertisements

Joined
Oct 20, 2010
Messages
1
Reaction score
0
How to Remove user controls?

protected void Page_Load(object sender, EventArgs e)
{
// Put user code to initialize the page here
//Default Selection is View Forecast

if (ViewState["ViewForecastFirstTime"] == null)
{


Controls.Add(LoadControl("ViewForecast.ascx"));


}

if (Request.Form["__EVENTTARGET"] == "btnUploadForecast")
{

Controls.Add(LoadControl("UploadForecast.ascx"));


}

if (Request.Form["__EVENTTARGET"] == "btnViewForecast")
{

Controls.Add(LoadControl("ViewForecast.ascx"));


}

if (Request.Form["__EVENTTARGET"] == "btnNewForecast")
{

Controls.Add(LoadControl("NewForecast.ascx"));


}

if (Request.Form["__EVENTTARGET"] == "btnPortView")
{

this.Controls.Add(LoadControl("PartView.ascx"));



}
}

Is this possible? How to remove the User control?
 

Top