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

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


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.