user1017882
user1017882

Reputation:

Failed to load viewstate?

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.

Upvotes: 0

Views: 3044

Answers (3)

nick1024
nick1024

Reputation: 1

To supress this error (ViewState will be loaded partically):

public partial class MyClass: System.Web.UI.Page
{ 
     bool StateFail;

     protected  override object LoadPageStateFromPersistenceMedium() {
        if (StateFail) return null;
        return base.LoadPageStateFromPersistenceMedium();
       }

    protected override void OnInitComplete(EventArgs e) {
        base.OnInitComplete(e);
        if (!IsPostBack) return;
        try
        {
            typeof(System.Web.UI.Page).GetMethod("LoadAllState", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).Invoke(this, new object[0]);
        } catch { StateFail = true; }
        }
}

}

Upvotes: 0

freefaller
freefaller

Reputation: 19953

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 .aspx and .ascx files. 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 .aspx and .ascx files to be in exactly the same state.

From what I can tell from your comments, you are doing is this...

  • Requesting the page from the server (which is loading the HTML, including the __VIEWSTATE)
  • Changing the .aspx page on the server (in this case adding a new <asp:Panel>)
  • Posting the page back to the server and getting an error.

In this third stage, the server is looking at the controls in the .aspx and 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 __VIEWSTATE mentions nothing about it.


So the solution to your problem is simply - just add the control to the .aspx before you load the page into the browser

Upvotes: 1

JohnnBlade
JohnnBlade

Reputation: 4327

You need to close the panel with </asp:Panel>

<asp:Panel runat="server" ID="pnlTest">
</asp:Panel>

create your dynamic controls on page init

for more info http://geekswithblogs.net/FrostRed/archive/2007/02/17/106547.aspx

and here some more info http://blog.typps.com/2008/01/failed-to-load-viewstate-typical.html

Upvotes: 2

Related Questions