Failed To Load Viewstate Error In Asp.net
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Failed to load viewstate. The control tree into which viewstate is being loaded up vote 6 down vote favorite 4 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. Thanks for any assistance or ideas! asp.net .net umbraco .net-4.5 asp.net-4.5 share|improve this question asked Aug 3 '12 at 12:44 krisdyson 1,91753373 this blog post helped me with most of my viewstate problems weblogs.asp.net/infinitiesloop/archive/2006/08/03/… –gsharp Aug 3 '12 at 12:58 add a comment| 10 Answers 10 active oldest votes up vote 17 down vote 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 b
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. The only way to resolve is a proper understanding of viewstate.http://geekswithblogs.net/FrostRed/archive/2007/02/17/106547.aspxis a interesting post on viewstate that i happen to read today, pointed out to me by someone who ran into a viewstate problem about the control tree not matching and was clearly afraid of http://stackoverflow.com/questions/11795994/failed-to-load-viewstate-the-control-tree-into-which-viewstate-is-being-loaded adding controls dynamically after reading some facts presented in that article. Who wouldn't ?While the post gives us a very good understanding of viewstate and how it can fail, so i encourage you to read it first, might seem lengthy but I assure you, it's quite interesting. However, when you're done, follow my rant here, since I feel it's important to know, that, http://blog.typps.com/2008/01/failed-to-load-viewstate-typical.html the failure can only happenwhen either donedeliberately as per the sample code in the post i linked to above orto *not* understanding viewstate and how it works.So how can we easily avoid these failures ? Let's look at his first code example,and build onto that :protected void Page_Init(object sender, EventArgs e){ if (!IsPostBack) { Button btnClickMe = new Button(); form1.Controls.Add(btnClickMe); btnClickMe.Text = "Click me"; } else { Label label = new Label(); form1.Controls.Add(label); }}As you can note above, this is problematic, since the control into which viewstate is restored is matched by control index, so when the index changes, as is clear in the above code, because if btnClickMe was loaded in for example index [0], now upon postback, after the page has been recreated and rebuilt, the Label "label" is loaded in index [0] instead and takes the place of the button. So this means viewstate that was meant for the button is loaded into the label instead, and the output in the screen after clicking the button is "click me" which was clearly not provided to the label's text property.Now that we understand the problem, how
Tips/Tricks Top Articles Beginner Articles Technical Blogs Posting/Update Guidelines Article Help Forum Article Competition Submit an article or tip Post http://www.codeproject.com/Questions/387870/Failed-to-load-view-state-error your Blog quick answersQ&A Ask a Question View Unanswered Questions View All Questions... C# questions Linux questions ASP.NET questions SQL questions VB.NET questions discussionsforums All Message Boards... Application Lifecycle> Running a Business Sales / Marketing Collaboration / Beta Testing Work Issues Design and Architecture ASP.NET JavaScript C / C++ / MFC> ATL / WTL failed to / STL Managed C++/CLI C# Free Tools Objective-C and Swift Database Hardware & Devices> System Admin Hosting and Servers Java .NET Framework Android iOS Mobile SharePoint Silverlight / WPF Visual Basic Web Development Site Bugs / Suggestions Spam and Abuse Watch features Competitions News The Insider Newsletter The Daily Build Newsletter Newsletter archive Surveys failed to load Product Showcase Research Library CodeProject Stuff communitylounge Who's Who Most Valuable Professionals The Lounge The Insider News The Weird & The Wonderful The Soapbox Press Releases Non-English Language > General Indian Topics General Chinese Topics help What is 'CodeProject'? General FAQ Ask a Question Bugs and Suggestions Article Help Forum Site Map Advertise with us About our Advertising Employment Opportunities About Us Ask a Question All Questions All Unanswered FAQ Failed to load view state error Rate this: Please Sign up or sign in to vote. See more: C# Hi, I have a isssue that on editing the gv it get edited and updates only once, Second tym i get this error: 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 p