如何修复此页面的状态信息无效,可能已损坏

时间:2010-08-13 10:51:17

标签: asp.net viewstate

我正在开发一个Asp.net项目,我通过页面对象上的LoadControl()动态地在页面上创建了一些用户控件,但当我尝试通过任何页面进行任何回发时,我得到"The state information is invalid for this page and might be corrupted."即使在我已将动态创建的控件的Viewstate设置为false。

如果您遇到此问题,请告诉我您是如何解决的。

堆栈跟踪是: -

[FormatException: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or a non-white space character among the padding characters. ]
   System.Convert.FromBase64String(String s) +0
   System.Web.UI.ObjectStateFormatter.Deserialize(String inputString) +77
   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() +147

[ViewStateException: Invalid viewstate. 

[HttpException (0x80004005): The state information is invalid for this page and might be corrupted.]
   System.Web.UI.ViewStateException.ThrowError(Exception inner, String persistedState, String errorPageMessage, Boolean macValidationError) +198
   System.Web.UI.ViewStateException.ThrowViewStateError(Exception inner, String persistedState) +14
   System.Web.UI.HiddenFieldPageStatePersister.Load() +251
   System.Web.UI.Page.LoadPageStateFromPersistenceMedium() +106
   System.Web.UI.Page.LoadAllState() +43
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +8431
   System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +253
   System.Web.UI.Page.ProcessRequest() +78
   System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) +21
   System.Web.UI.Page.ProcessRequest(HttpContext context) +49
   ASP.contact_us_aspx.ProcessRequest(HttpContext context) in c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\otherland_ecom\33f31476\6c5f9007\App_Web_zgmfrrfa.14.cs:0
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +100
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75

4 个答案:

答案 0 :(得分:2)

在设计页面(.aspx)中将EnableEventValidation设置为false

<%@ Page Title="Home Page" EnableEventValidation="false" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>

答案 1 :(得分:1)

请您确保在Page_Load之前加载动态控件。您可以在Page_Init中加载它们。 (无需分配值,因为它们将从视图状态分配)。

答案 2 :(得分:0)

我认为你已经在page_load方法之后添加了控件。 当你添加控件时,它会添加隐藏字段,当页面回发时它不会与初始隐藏字段匹配,因此它会将错误上升到动态添加控件时尝试删除其隐藏字段然后它将解决您的问题问题

答案 3 :(得分:-1)

我已经将Viestate存储换成了Session,这个问题就消失了。当会话过期时,我的登录过期,因此页面被强制重新加载并保持有效:

//ViewState["startConversion"] = false;
Session["startConversion"] = false;

或者

<asp:HiddenField ID="hfStartConversion" runat="server" />