错误的细节如下.此错误仅偶尔/很少/有时发生,并且没有任何步骤来重现它.
我怎么知道哪个控件抛出了这个Viewstate错误?
错误消息:无法加载视图状态.正在加载视图状态的控制树必须与在上一个请求期间用于保存视图状态的控制树匹配.例如,在动态添加控件时,在回发期间添加的控件必须与初始请求期间添加的控件的类型和位置相匹配.
时间:很少/偶尔
重新创建的步骤:不能
堆栈跟踪:
System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.Web.HttpException: 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. at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Page.LoadAllState() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) --- End of inner exception stack trace --- at System.Web.UI.Page.HandleError(Exception e) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest() at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) at System.Web.UI.Page.ProcessRequest(HttpContext context) at ASP.sessions_aspx.ProcessRequest(HttpContext context) in c:\windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\73694782\85423fe4\App_Web_mnrmfsfa.8.cs:line 0 at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
amartynov.. 24
遗憾的是,无法确定哪个控制层次结构中没有正确添加的有罪控件.
有!
在调试设置中禁用"仅我的代码".并捕获所有抛出的异常(在Ctrl-Alt-E打开的对话框中检查公共语言运行时'抛出').
发生异常后,转到Control对象可用的最近堆栈帧并检查其ClientID和父项.
遗憾的是,无法确定哪个控制层次结构中没有正确添加的有罪控件.
有!
在调试设置中禁用"仅我的代码".并捕获所有抛出的异常(在Ctrl-Alt-E打开的对话框中检查公共语言运行时'抛出').
发生异常后,转到Control对象可用的最近堆栈帧并检查其ClientID和父项.
更常见的是,当页面的控件层次结构以阻止框架加载视图状态的方式更改时,会发生此错误.视图状态机制假定控制层次结构在加载时与保存时相同.
这可能看起来像一个随机错误,因为有些情况下更改页面中的控件并不会阻止加载视图状态.查看您的页面并查找动态创建/删除的控件.确保在加载视图状态之前将任何控件添加到页面中(即在页面Load事件之前).
遗憾的是,无法确定哪个控制层次结构中没有正确添加的有罪控件.查看此错误的常见方法是在事件上添加一些动态控件(例如,在下拉列表中选择的索引已更改) - 这样他们的状态将保存到视图状态 - 但不会在回发时再次添加它们 - 这样视图state无效,因为加载视图状态时控件不再存在.
此错误有一种解决方法:
将所有动态加载控件的EnableViewstate属性设置为false.这不是一个完整的解决方案; 我们在这里禁用控件的ViewState以绕过此错误.
此解决方案可能导致其他错误抬起头,因此在应用此修复时要小心.