C# 无法加载视图状态。只是偶尔发生。很难重现

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/580704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 09:06:53  来源:igfitidea点击:

Failed to load viewstate. Happening only occasionally. Tough to recreate

c#asp.netajaxviewstate

提问by

Details of Error are given below. This error happens only occasionally / rarely / sometimes and there aren't any steps to reproduce it.

错误的详细信息如下。此错误仅偶尔/很少/有时发生,并且没有任何步骤可以重现它。

How can I know which Control is throwing this Viewstate error?

我怎么知道哪个控件抛出了这个 Viewstate 错误?

Error Message: 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.

错误消息:无法加载视图状态。正在加载视图状态的控件树必须与在前一个请求期间用于保存视图状态的控件树匹配。例如,动态添加控件时,回发期间添加的控件必须与初始请求期间添加的控件的类型和位置相匹配。

When: Rarely / Occasionally

时间:很少/偶尔

Steps to Recreate: Can't

重新创建的步骤:不能

Stack Trace:

堆栈跟踪:

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\root694782423fe4\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)

回答by Chuck Conway

I've experienced this error, it happens when there is a cross page postback.

我遇到过这个错误,它发生在跨页回发时。

For example: You load View A. View A loads fine, for whatever reason the conditions under which View A loaded, no longer exist. The fallback page is View B. So the user completes the form on View A and postsback. Since View A's conditions are no longer met, View A's form values are posted to View B.

例如:您加载视图 A。视图 A 加载正常,无论出于何种原因,加载视图 A 的条件不再存在。回退页面是视图 B。因此用户在视图 A 上完成表单并回发。由于不再满足视图 A 的条件,因此视图 A 的表单值将发布到视图 B。

回答by Aleris

More generally this error happens when the control hierarchy of the page changesin a way that prevents the framework to load the view state. The view state mechanism assumes that the control hierarchy is the same on load as it was when it was saved.

更一般地,当页面控件层次结构以阻止框架加载视图状态的方式发生更改时,会发生此错误。视图状态机制假定控件层次结构在加载时与保存时相同。

This mightlook as a random error because there are cases when changing the controls in the page does not prevent the view state from loading. Have a look in your page and look for controls that are dynamically created/deleted. Ensure that any controls are added to the page beforethe view state is loaded (that would be before page Load event).

可能看起来像是一个随机错误,因为在某些情况下,更改页面中的控件不会阻止加载视图状态。查看您的页面并查找动态创建/删除的控件。确保加载视图状态之前(即在页面 Load 事件之前)将任何控件添加到页面。

Unfortunately there is no way to see which exactly is the guilty control that is not added correctly to the control hierarchy. A common way to see this error is to add some dynamic controls on an event (for example on a drop down selected index changed) - this way their state is saved to view state - but not add them again on postback - this way the view state is invalid because the controls do not exists any more when the view state is loaded.

不幸的是,没有办法查看到底哪个是没有正确添加到控件层次结构中的有罪控件。查看此错误的常见方法是在事件上添加一些动态控件(例如,在更改的下拉选定索引上) - 这样它们的状态将保存到视图状态 - 但不会在回发时再次添加它们 - 这样视图state 无效,因为在加载视图状态时控件不再存在。

回答by amartynov

Unfortunately there is no way to see which exactly is the guilty control that is not added correctly to the control hierarchy.

不幸的是,没有办法查看到底哪个是没有正确添加到控件层次结构中的有罪控件。

There is!

有!

Disable "Just my code" in debugging settings. And catch all thrown exceptions (check 'thrown' for Common Language Runtime in the dialog opened by Ctrl-Alt-E).

在调试设置中禁用“仅我的代码”。并捕获所有抛出的异常(在 Ctrl-Alt-E 打开的对话框中检查公共语言运行时的“抛出”)。

After the exception occurs, go to the nearest stack frame where the Control object is available and examine its ClientID and parents.

发生异常后,转到最近的 Control 对象可用的堆栈帧并检查其 ClientID 和父级。

回答by Daniel Auger

I've seen this error when using a gridview. It would happen when user interaction caused a postback before the page was done rendering completely.

我在使用 gridview 时看到过这个错误。在页面完全渲染完成之前用户交互导致回发时会发生这种情况。

I believe it was fixed in VS2005 SP1 however.

不过,我相信它已在 VS2005 SP1 中修复。

回答by Noon Silk

You can probably reproduce this by stoppingthe page before it is fully loaded, and thensubmitting a postback. If the server wasn't able to get the complete viewstate from the server, then it won't be able to re-create it on postback, hence the crash.

您可以通过在页面完全加载之前停止页面,然后提交回发来重现这一点。如果服务器无法从服务器获取完整的视图状态,那么它将无法在回发时重新创建它,从而导致崩溃。

回答by Raje

There is a workaround for this error:

此错误有一个解决方法:

Set EnableViewstate property of all dynamically loading controls to false. This is not a complete solution; we are disabling controls' ViewState here to bypass this error.

将所有动态加载控件的 EnableViewstate 属性设置为 false。这不是一个完整的解决方案;我们在此处禁用控件的 ViewState 以绕过此错误。

This solution can cause other errors to raise their heads, so be careful when applying this fix.

此解决方案可能会导致其他错误抬头,因此在应用此修复程序时要小心。

回答by MsBugKiller

I had this error when my custom controls were accidentally created in Page_Load(). Controls should be dynamically created in OnInit().

当我的自定义控件意外地在 Page_Load() 中创建时,我遇到了这个错误。控件应该在 OnInit() 中动态创建。

回答by Mad Dog Tannen

I experienced the exact same problem. I had 2 listview in a Master page, associated with 2 updatedpanels.

我遇到了完全相同的问题。我在母版页中有 2 个列表视图,与 2 个更新的面板相关联。

Disabling the EnableViewState on the Listviews solved it for me!

禁用 Listviews 上的 EnableViewState 为我解决了这个问题!