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

半城伤御伤魂 提交于 2019-11-28 09:08:26

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

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

More generally this error happens when the control hierarchy of the page changes in 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 might look 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 before the view state is loaded (that would be before page Load event).

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.

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.

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

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.

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

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.

I believe it was fixed in VS2005 SP1 however.

You can probably reproduce this by stopping the page before it is fully loaded, and then submitting 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.

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

Disabling the EnableViewState on the Listviews solved it for me!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!