Failed to load viewstate. The control tree into wh

2019-01-23 15:57发布

I am receiving the following error message after an HTTP POST on an ASP.NET form hosted inside a UserControl:

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.

Here's additional info:

  • I'm running .NET 4.5 RC
  • It's an Umbraco 4.7-based website
  • On my local dev machine the form works perfectly
  • This error only occurs on the staging server which has .NET 4.5 (only), MSSQL 2012 Express, IIS 7.5, Windows 7 (I know, it's not a real server yet, one day maybe...)
  • The server is not part of a web farm (or garden, tho that should be irrevelant)
  • The user control does render controls dynamically

I have applied all the latest service packs. I have run out of ideas now! I have even restarted it and also performed a richual over the server involving a song and a special dance to no avail.

10条回答
狗以群分
2楼-- · 2019-01-23 16:07

You can add new PlaceHolder per UserControls

OR

You can set enableviewstate=false on the control , if you dont need viewstate

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-01-23 16:10

In my case I had a grid view with (OnPageIndexChanging) event and when I click on a page nothing will happen until I click it twice!

I was refreshing the data source before setting new page index.


This is what I was doing wrong

grd.DataSource = data;
grd.DataBind();
grd.PageIndex = e.NewPageIndex;

This is the right way

grd.PageIndex = e.NewPageIndex;
grd.DataSource = data;
grd.DataBind();
查看更多
Ridiculous、
4楼-- · 2019-01-23 16:15

What is important when you are adding controls dynamically is on which event you are adding them.

If you added controls on events that occur after load, they will be part of the viewstate you send to the client.

You will have to add those controls again before LoadViewState is called.

If you run into cases where the decision of which controls to add is itself stored in the ViewState or the value of a control, then remember even before the ViewState is loaded, this data is available in Request.Params

Refer the asp.net page life cycle

Page life cycle

查看更多
Emotional °昔
5楼-- · 2019-01-23 16:15

OK, so the answer is literally: "Set up a new server with all the same software as the last one and try again" and it works now.

查看更多
成全新的幸福
6楼-- · 2019-01-23 16:18

This can happen if you override SaveViewState in your control but don't override LoadViewState.

查看更多
男人必须洒脱
7楼-- · 2019-01-23 16:20

Check if you have the binding method of the control directly in your page load event. This can cause this problem.

查看更多
登录 后发表回答