动态添加控件后状态信息损坏

时间:2009-06-10 05:40:24

标签: c# .net asp.net

在我的asp .net表单中,我通过AjaxPro库使用Ajax刷新控件。但是,在这样做之后......导致任何“正常”回发结果出现黄色屏幕(消息后出现完整错误)

我在web.config中设置了以下页面属性而没有任何运气

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

我也尝试过生成一个机器密钥,正如一些解决方案在线建议的那样。但这也无济于事。

有什么建议吗?

******************* ASP .NET黄色屏幕的内容********************* ***************

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.]

5 个答案:

答案 0 :(得分:5)

我发现了问题。为了响应AjaxPro库的ajax请求,我像这样渲染控件 -

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();
}

这种方法的问题是asp .net 总是 为viewstate添加了一个隐藏的输入字段。根据您的页面/控件包含的控件,添加了一些其他隐藏字段,如eventtarget,eventargument,eventvalidation。然后,当我将生成的html附加到客户端的现有DOM元素时,很明显有重复的隐藏输入字段具有相同的名称和id,导致视图状态损坏。

解?

使用正则表达式(如果你擅长)或字符串比较/搜索等功能剥离这些标签的生成的html。

答案 1 :(得分:1)

用于替换视图状态的正则表达式: returnvalue = Regex.Replace(html,@“&lt; [/]?(form | [ovwxp]:\ w +)[^&gt;] *?&gt;”,“”,RegexOptions.IgnoreCase);

答案 2 :(得分:0)

看起来可能会截断回发请求。你能检查一下ViewState的大小吗?

答案 3 :(得分:0)

每个页面中有两个form标记,删除其中一个标记并解析:

<%@ 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>

答案 4 :(得分:0)

将此行添加到您的cs文件中,它将从输出html中删除viewstate字段:

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);
    }