Failed to load viewstate when page contains viewstate variables

I got this error when I do not use or process pages that contain a viewstate for a couple of minutes and reload the page:

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.

I have a solution by setting the page parameter enableviewstate = false

but after applying this, the viewstate variable does not work on this page.

+4
source share
2 answers

In my case, this problem arose because I was creating a control that affected the value of the control position, because it could not close the visualization of the tag for this control. correcting the check tag, it was fixed.

0
source

The reason may be that you are dynamically adding controls to Page_Load. That is, you add some mesh to Page_Load, and its view state is saved on the page, but then when the page is sent back and the ViewState is parsed, the engine cannot find the appropriate controls for the parsed ViewState.

This is due to the fact that the boot event (which is processed by Page_Load) is triggered after the page is fully loaded, i.e. children are created, the viewing state is analyzed and applied, but before client events, for example, when a button is pressed and is displayed on the page.

Move the addition of dynamic controls to Page_Init and make sure that they are exactly the same each time they are created, that is, when you first start the page, and when you start the page after the postback.

Useful Link ASP.NET Page Life Cycle

+1
source

All Articles