abjbhat
abjbhat

Reputation: 1019

State information corrupted after dynamically adding control

In my asp .net form, I refresh a control using Ajax through the AjaxPro library. However, after doing so...causing any "normal" postback results in a yellow screen (Complete error after the message)

I've set the following Page properties in web.config without any luck

<pages enableSessionState="true" enableViewState="false" enableEventValidation="false" enableViewStateMac="false">

I've also tried generating a machine key as some solutions suggested online. But that doesn't help either.

Any suggestions?

*******************Contents of ASP .NET yellow screen************************************

Server Error in '/' Application.
The state information is invalid for this page and might be corrupted.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: The state information is invalid for this page and might be corrupted.

Source Error:

[No relevant source lines]


Source File: c:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\c89baa89\d92b83c5\App_Web_tahnujzf.4.cs    Line: 0

Stack Trace:

[FormatException: Invalid character in a Base-64 string.]
   System.Convert.FromBase64String(String s) +0
   System.Web.UI.ObjectStateFormatter.Deserialize(String inputString) +72
   System.Web.UI.ObjectStateFormatter.System.Web.UI.IStateFormatter.Deserialize(String serializedState) +4
   System.Web.UI.Util.DeserializeWithAssert(IStateFormatter formatter, String serializedState) +37
   System.Web.UI.HiddenFieldPageStatePersister.Load() +113

[ViewStateException: Invalid viewstate. 
    Client IP: 127.0.0.1
    Port: 49736
    User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10 (.NET CLR 3.5.30729)
    ViewState: /wEPDwULLTE0OTczNDQ1NjdkGAIFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYGBSljdGwwMCR1c3JTaG9wcGluZ0xpc3QkdXNyU2hvcHBpbmdMaXN0X3duZAUaY3RsMDAkdXNyU2hvcHBpbmdMaXN0JF9tZ3IFVGN0bDAwJFBsYWNlSG9sZGVyTWFpbiRjdGwwMCRfJHVzclZpZXdDYXJ0Q29udHJvbCRfJGN0bDAwJF8kcnB0TGluZUl0ZW1zJGN0bDAwJGNieEFsbAVcY3RsMDAkUGxhY2VIb2xkZXJNYWluJGN0bDAwJF8kdXNyVmlld0NhcnRDb250cm9sJF8kY3RsMDAkXyRycHRMaW5lSXRlbXMkY3RsMDEkaW1nQnRuQ2F0ZWdvcnkFWmN0bDAwJFBsYWNlSG9sZGVyTWFpbiRjdGwwMCRfJHVzclZpZXdDYXJ0Q29udHJvbCRfJGN0bDAwJF8kcnB0TGluZUl0ZW1zJGN0bDAxJGNieFNlbGVjdGlvbgVoY3RsMDAkUGxhY2VIb2xkZXJNYWluJGN0bDAwJF8kdXNyVmlld0NhcnRDb250cm9sJF8kdXNyUHJvZHVjdFF1aWNrVmlld1BvcHVwJHVzclByb2R1Y3RRdWlja1ZpZXdQb3B1cF93bmQFG2N0bDAwJFBsYWNlSG9sZGVyTWFpbiRjdGwwMA8XAgULQ3VycmVudFZpZXcFCFZpZXdDYXJ0BQ1BY3RpdmVWaWV3U2V0Z2Q=,/wEPDwUBMGQYAQUeX19Db250cm9sc1JlcXVpcmVQb3N0QmFja0tleV9fFgMFIWN0bDAxJF8kc...]

[HttpException (0x80004005): The state information is invalid for this page and might be corrupted.]

Upvotes: 0

Views: 4218

Answers (5)

Homayoon Ahmadi
Homayoon Ahmadi

Reputation: 2833

add this lines to your cs file and it will remove viewstate field from output html:

protected override void Render(HtmlTextWriter output)
    {
        StringWriter stringWriter = new StringWriter();

        HtmlTextWriter textWriter = new HtmlTextWriter(stringWriter);
        base.Render(textWriter);

        textWriter.Close();

        string strOutput = stringWriter.GetStringBuilder().ToString();

        strOutput = Regex.Replace(strOutput, "<input[^>]*id=\"__VIEWSTATE\"[^>]*>", "", RegexOptions.Singleline);

        output.Write(strOutput);
    }

Upvotes: 0

mosayeb
mosayeb

Reputation: 1

you have two form tags in each page, remove one of them and it resolves:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="test.ascx.cs" Inherits="shop_test" %>
<asp:Panel runat="server" ID="basketpanel" Visible="True">
    <form id="Form1" runat="server">
    </form>
    </asp:Panel>
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="test.ascx.cs" Inherits="shop_test" %>
<asp:Panel runat="server" ID="basketpanel" Visible="True">
    <form id="Form1" runat="server">
    </form>
</asp:Panel>

Upvotes: 0

Hariharans87
Hariharans87

Reputation: 11

Regular Expression for replace the viewstate: returnvalue = Regex.Replace(html, @"<[/]?(form|[ovwxp]:\w+)[^>]*?>", "", RegexOptions.IgnoreCase);

Upvotes: 1

abjbhat
abjbhat

Reputation: 1019

I found the problem. In response to an ajax request with the AjaxPro Library, I render controls like so-

Page page = new Page();

/*Not the exact way I init the control. But that's irrevelant*/
Control control = new Control();
page.Controls.Add(control)
string controlHtml;

using(StringWriter sw = new StringWriter())
{
   HttpContext.Current.Server.Execute(page, sw, false);
   controlHtml = sw.ToString();
}

The problem with this approach is that asp .net ALWAYS adds a hidden input field for viewstate. Some other hidden fields like eventtarget, eventargument, eventvalidation are added depending on the controls your page/controls contains. Then when I append the resulting html to an existing DOM element on the client side, quite obviously there are duplicate hidden input fields with the same name and id resulting in the view state corruption.

Solution?

Strip the generated html of these tags using regex (if you're good at it) or string compare/search etc functions.

Upvotes: 5

Per Erik Stendahl
Per Erik Stendahl

Reputation: 883

Looks like the postback request might be truncated. Can you check the size of the ViewState?

Upvotes: 0

Related Questions