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,
<asp:DropDownList ID="DropDownList1" runat="server" EnableViewState="false">
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
That’s all, No more ViewState issue! Happy Programming!!
ASP.NET, C#, Failed to load viewstate, GridView, Step By Step, StepBiStep, VB.Net, ViewState
This is not really new but currently I am working on some customized web parts, so I need to turn on/off the detailed error messages when I need to fix some hectic problems on my development environment. There are many posts regarding this, but I wanted to keep my own post for my future references. 😉
Normally SharePoint shows custom error messages to the users such as “An unexpected error has occurred” but this is not going to help, so we need to know the detailed ASP.NET error message to fix the problem. Here are the steps to turn off custom error messages.
Step 1: Go to the SharePoint host server and navigate to the Virtual Directories folder. Normally this is available in this path “C:\Inetpub\wwwroot\wss\VirtualDirectories”
Step 2: Find the web.config and get the backup for the safety purpose
Step 3: Open the web.config file using Visual Studio OR Notepad
Step 4: Find the “CallStack” and change the value to “true” instead of “false”
Step 5: Find the “CustomErrors” and change the value to “Off” instead of “On”
Step 6: Save and close the web.config, refresh the page then you will get the detailed (wired) error message. Time to fix this huh? 🙂
Error message before turning on:
Error message after turning on:
MSDN reference shows the “CustomErrors” values can be one of these:
||Specifies that custom errors are enabled. The custom errors are shown to the remote clients and to the local host.
||Specifies that custom errors are disabled. The detailed ASP.NET errors are shown to the remote clients and to the local host.
||Specifies that custom errors are shown only to the remote clients, and that ASP.NET errors are shown to the local host.
If you want, you can also set the “CustomErrors” value to “RemoteOnly” to show custom errors only to the remote clients.
ASP.NET, CallStack, CustomErrors, error messages, RemoteOnly, SharePoint 2007, SharePoint 2010, Turn on, Virtual Directories