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.
The page loads fine, until I include an asp panel on the page. Then I see the above error.
Can anyone think of a possible cause for this? The asp panel is empty, I’ve literally just created a test:
<asp:Panel runat="server" ID="pnlTest"></asp:Panel>
And it breaks.
The problem you are experiencing is down to you changing things mid-way through the process…
When you request a page from ASP.NET, it uses the mark-up in the
.aspxand.ascxfiles. From this (along with lots of other things, including any dynamic controls you create in the code-behind) it creates the ViewState, which is stored in your page in a hidden field called__VIEWSTATE.When you then subsequently post-back the page to the server (through a link, or a button click, etc) the server will load that ViewState information, and expect all the controls in the
.aspxand.ascxfiles to be in exactly the same state.From what I can tell from your comments, you are doing is this…
__VIEWSTATE).aspxpage on the server (in this case adding a new<asp:Panel>)In this third stage, the server is looking at the controls in the
.aspxand looking at the information in the__VIEWSTATE… the error is because the controls do not match!The server is not expecting to see an
<asp:Panel>in the page, because the__VIEWSTATEmentions nothing about it.So the solution to your problem is simply – just add the control to the
.aspxbefore you load the page into the browser