I have got the following error when I was developing a gridview editing on a webpage last week. This is most common issue, even I have faced several times earlier. But I want to keep this solution for beginners!
Here is the full 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"
My 1st suggestion is just check the cache! Meaning..clear your browser cache and refresh it and see whether it is working? if not continue with one of the following solutions!
1. Enable ViewState is false
Find the control which is giving problem in the page and then disable the ViewState! For me it was a DropDownList so,
<EditItemTemplate> <asp:DropDownList ID="DropDownList1" runat="server" EnableViewState="false"> </asp:DropDownList> </EditItemTemplate>
2. Just place the data binding on the GridView events wherever you use the edit! For and example “RowCancelingEdit”
Protected Sub GridViewRelease_RowCancelingEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs) Handles GridViewRelease.RowCancelingEdit GridViewReleaseAnalyst.EditIndex = -1 BindData() End Sub
That’s all, No more ViewState issue! Happy Programming!!