Failed to load viewstate. The control tree into which viewstate is being loaded

Viewed 79604

I am receiving the following error message after an HTTP POST on an ASP.NET form hosted inside a UserControl:

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.

Here's additional info:

  • I'm running .NET 4.5 RC
  • It's an Umbraco 4.7-based website
  • On my local dev machine the form works perfectly
  • This error only occurs on the staging server which has .NET 4.5 (only), MSSQL 2012 Express, IIS 7.5, Windows 7 (I know, it's not a real server yet, one day maybe...)
  • The server is not part of a web farm (or garden, tho that should be irrevelant)
  • The user control does render controls dynamically

I have applied all the latest service packs. I have run out of ideas now! I have even restarted it and also performed a richual over the server involving a song and a special dance to no avail.

13 Answers

In my case I was manipulating the .Text property of a asp:Literal on page load which was causing the issue. In all other cases this never caused me a viewstate error but in this particular case I was changing the .Text value to an html element.

The following caused the error:

<asp:Literal ID="SvgIcon" runat="server" />

SvgIcon.Text = "<svg version=\"1.1\" id=\"Layer_1\" bla bla />"

I was able to resolve the error by adding EnableViewState="false" explicitly to the control:

<asp:Literal ID="SvgIcon" runat="server" EnableViewState="false" />

This can happen if you override SaveViewState in your control but don't override LoadViewState.

So I actually ended up discovering that the list of entities I was binding to was not in the same order as the controls in ViewState! I'm still working thru a cleaner solution, but my code is working with ViewStateEnabled = true by having the method which reconstructs my dynamic controls (called from Page_Load) do it differently if !IsPostBack.

Ultimately, I will probably need to fix my sorting algorithm for my nested dynamic controls, but suffice it to say: if you are using the same pattern as I am, of using a List to generate/bind to dynamic controls, and that order is fluid or changing, try comparing Request.Params to find the keys that are relevant to your control hierarchy, and see if they match the order of your List. That solved my issue. Kudos to @nunespascal!

In short, I am dynamically generating all but one tab in an AjaxToolkit tab control, and then populating that with a couple layers deep of placeholders and regular controls (textboxes, dropdownlists, etc), so that's why it's complicated to get the order of everything correct.

Although this is very old question, I had visited this as I got the similar issue. But my issue was generated just because I have added a javascript code in Master page in head tag. That javascript code is reading a value of Session["KeyName"] ,

Code is like below -

$(document).ready(function () {
    var allowOpenInNewTab = false;
    allowOpenInNewTab = '<%# Convert.ToString(Session["AllowOpenInNewTab"]).ToLower() %>' == 'true';

    if (!allowOpenInNewTab && window.sessionStorage.tabId != '1') {
        alert("This page is not allowed to be open in another tab, sorry we can not load the page!!");                
    }
});

When I remove above code then everything was running smoothly but if I keep adding this part of code, it was giving this error of

Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate...

Finally I found the solution like if I move my javascript code from head to just before the end of the body tag.

So solution that worked for me was moving javascript code (which is reading Session value from Server tags) to just before end of body tag.

Related