Tyler Miranda
Tyler Miranda

Reputation: 433

Failed to load viewstate

I have an interesting viewstate problem here that I'm having trouble ironing out. Granted I'm not an expert at dealing with the viewstate, it is probably one of the most confusing theings about .NEt but that is neither here no there. Here is the error I'm getting.

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.

What is weird about this is that it only happens on the homepage of the site (code is located in footer of master page). Every other page on the site it works fine. Here is the aspx code. Note this is in the footer of the master file.

<asp:UpdatePanel ID="upEmail" runat="server" >
  <ContentTemplate>
    <asp:MultiView ID="mview" runat="Server" ActiveViewIndex="0">

      <asp:View ID="viewInput" runat="Server">
        <div>
          <asp:Panel ID="pnlEmail" runat="server" DefaultButton="btnSubscribe" Width="100%" EnableViewState="False">
            <asp:TextBox ID="tbEmail" runat="server" Width="125px" CssClass="someClass" title="Enter Your Email Address" />
            <ajaxToolkit:TextBoxWatermarkExtender ID="TextBoxWatermarkExtender1" TargetControlID="tbEmail" WatermarkText="Email Address" runat="server" />
            <asp:RequiredFieldValidator ID="rfEmail" runat="Server" ControlToValidate="tbEmail" ValidationGroup="submit">*</asp:RequiredFieldValidator>
            <asp:ImageButton ID="btnSubscribe" ImageUrl="/Images/social/signup.gif" Imagealign="absmiddle" runat="server" OnClick="BtnSubscribe_Clicked" ValidationGroup="submit" />
            <br />
            <asp:RegularExpressionValidator ID="regEmail" runat="server" ControlToValidate="tbEmail" ErrorMessage="Please enter a valid email." ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ValidationGroup="submit" Display="dynamic" ForeColor="" CssClass="error_font_no_hover" />
          </asp:Panel>
        </div>
        <div>
          <asp:Label ID="lblMessage" runat="server" Visible="false" />
        </div>
      </asp:View>

      <asp:View ID="viewSuccess" runat="server">
        Thank You! You will now
        <br />
        receive e-mails!
      </asp:View>

    </asp:MultiView>
  </ContentTemplate>
</asp:UpdatePanel>

I've tried disabling the viewstate on the panel and the error still occurs. Can anyone tell me what is going on?

Thank You in advance

Upvotes: 0

Views: 2080

Answers (2)

Joel Etherton
Joel Etherton

Reputation: 37533

You very likely have a misunderstanding of the control tree. For reference, I recommend the advice of Scott Guthrie:

http://weblogs.asp.net/alessandro/archive/2008/01/04/failed-to-load-viewstate-typical-problem-with-an-obvious-solution.aspx

Upvotes: 2

XtremeBytes
XtremeBytes

Reputation: 1497

Do you dynamically create any control in your home page? This error may be because you create controls dynamically in the code behind on the inital load but does not happen in the postback. If you have any control that are created dynamically make sure you do that in the OnInit method,

Upvotes: 1

Related Questions