Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate
Solution 1
In your case Anders, you still need to add the old control to your page in the init method along with the new control that you now want to add. Keep a reference to this old control that you have just added in a class level variable. So something like
Control _oldControl = null;
protected void Init_Page(object sender, EventArgs e)
{
//Code to decide which wuc to load.
UserControl wucc = (UserControl)Page.LoadControl(sFilePath);
ParentControl.ContentTemplateContainer.Controls.Add(wucc);
_oldControl = wucc as Control;
//Now add the new control here.
}
//override the LoadViewState method and remove the control from the control's collection once you page's viewstate has been loaded
protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
ParentControl.ContentTemplateContainer.Controls.Remove(_oldControl);
}
Hope this helps. If it did, please check the checkbox next to this answer to accept it and vote it up if you like :)
Solution 2
In order to avoid ViewState related errors please make absolutely sure that in Page_Init you create the same control tree that was created the previous time ViewState was saved i.e. the previous postback. Simple page life cycle:
Page Init - create the control tree - View State is loaded and applied here
Page Load - already loaded view state, you can do modifications to the control tree here - Save View State
Page PreRender
Solution 3
For what it’s worth I recently had the same problem.
My scenario was as follows.
A fixed panel of filters (dropdown lists and textboxes) which built a search SQL string. On submission of the search consequent results were displayed in an editable gridview beneath.
On editing the gridview I cold effectively change the state of a database record thus removing it from the gridview under the filters previously chosen. In some cases this resulted in no results being returned thus causing me to hide the gridview.
I then found that if I used the new state of the record in the filter and resubmitted the search that error sometimes occurred.
The problem I eventually found had nothing to do with enabled viewstates etc but simply that the empty gridview, though no longer visible (changed programmatically), had not been rebound to a null datasource.
This appeared to cause the conflict and the error.
So it appears as though in my case the viewstate issue arose from a non-visible gridview that contained non-refreshed data.
![Anders](https://i.stack.imgur.com/MPSfM.jpg?s=256&g=1)
Comments
-
Anders almost 4 years
I'm currently working on a dynamic core for several webprojects. It has a core that uses a treeview and a menu. And then for each specific projekt it loads several different wuc into a maincontent. Some business projects use business related wucs while others uses different ones. So the span of wuc's is really big.
Now to my problem, whenever a user press a menuitem or a treeitem it loads a wuc to the maincontent linked to that object.
But I'm having some viewstate errors and i've been looking around for 2 days now and none of the solutions explained are working for my projekt.
All my wuc has to have viewstate enabled.
Cycle is ->
Page(Control A) does postback with variable to change control to ControlB in wucPanel(UpdatePanel). OnLoad LoadRequested Wuc.
Current code is
protected void Load_Page(object sender, EventArgs e) { //Code to decide which wuc to load. UserControl wucc = (UserControl)Page.LoadControl(sFilePath); ParentControl.ContentTemplateContainer.Controls.Add(wucc); }
I've tried several fixes like adding diffrent ids to the wuc, but this either disabels the internal functions of control like handlers etc or generates the same viewstate error.
One solution i found was to load ControlA and then just removing it and then load ControlB. But this disabled the scripts for my 3rd party controller (Telerik).
I've also read about having diffrent PlaceHolders for each typof but since i expect havign up to 50 diffrent Controls I don't feel this is gonna help me.
And moving from Page_Load -> Page_Init generated the same 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.
-
Anders over 13 yearsProblem is i don't want to display the same control when the new life cycle starts. Since the user has requested a new control to be placed in the container where the old one used to be(in the UpdatePanel).
-
Anders over 13 yearsWould also like to point out that i implemented a solution that was pointed out on the following blog: geekswithblogs.net/FrostRed/archive/2007/02/19/… but it didnt solve my issue, i somehow still get this error!
-
Anders over 13 yearsi can't get the system to go into my overrided method, dont i just put this in my aspx.cs ? Or is there some other step?
-
Nikhil over 13 yearsWhich override method are you talking about? The init page or the LoadViewState?
-
Nikhil over 13 yearsIt will only be called on postbacks and not initial loads of the pages. Can you verify if it is being called on postbacks or not?
-
Anders over 13 yearsalthough it isnt exacly as I wanted it I beleive this is the way to go. Still getting telerik errors tho which I'll have to work around somehow.. Thanks for help!
-
Nikhil over 13 yearsIt is always tricky working with dynamic controls. I do a lot of crazy work with dynamically adding/removing controls actually. I have found this approach the best way to do it. Add the controls on Init first and then track them in LoadViewState. What errors are you getting with Telerik? I might be able to help.