Note: This article was originally published in 2009. Some steps, commands, or software versions may have changed. Check the current .Net documentation for the latest information.
Currently I am having the issue that when I create dynamic controls, even though I re-create them they still lose their respective ViewState. How can I ensure that my Dynamic ASP.Net controls and User Web Controls save their ViewState?
When a dynamic control is added to another control, the recently added dynatic control plays catch-up to get to the stage that the parent control is in (see attached picture at the bottom. Obtained from the (http://msdn.microsoft.com/en-us/magazine/cc163725.aspx “Asynchronous Pages in ASP.NET 2.0”)). This is important beceause at one point controls begin tracking their viewstate. The main issue at hand is that values added before the control is tracking viewstate won’t make it to viewstate and will be lost on PostBack. (http://technology.bauzas.com/files/2009/01/Post0049-cc163725.fig02en-us.gif.gif)](http://technology.bauzas.com/files/2009/01/Post0049-cc163725.fig02en-us.gif.gif) So what works and what doesn’t? If you already loaded your page the form is already tracking view state so if you add the dynamic control to the control collection before you set the values (by binding or setting the properties directly) then your changes will be tracked by the ViewState. You will find it doesn’t work however, if you set the values of the dynamic controls before you add them to the form’s controls collection. Here are some examples: Incorrect method: TextBox myTextBox = new TextBox(); if(!IsPostBack) { // This value wil not persist as it is being set before the Textbox is // added to the ControlCollection myTextBox.Text = “Value that I want to persist via ViewState”; } this.Controls.Add(myTextBox); Correct method: TextBox myTextBox = new TextBox(); // TextBox is added to the ControlCollection. It will catch up to the state // where it can maintain a ViewState this.Controls.Add(myTextBox); if(!IsPostBack) { // This value wil persist myTextBox.Text = “Value that I want to persist via ViewState”; }
Important Additional Note:
By adding a control BEFORE you set its properties, you allow for it to participate not only in ViewState but in the rest of the page lifecycle as partially shown above through the diagram.
Additional Resources:
- MSDN Library: (https://jcbauza.com/forums/EditPost.aspx/ASP.NET%20Page%20Life%20Cycle%20Overview “http://msdn.microsoft.com/en-us/library/ms178472.aspx”)
- MSDN Library: (http://msdn.microsoft.com/en-us/library/ms972976.aspx#viewstate_topic4 “Understanding ASP.NET View State”)
- MSDN Library: (http://msdn.microsoft.com/en-us/library/system.web.ui.usercontrol.loadviewstate.aspx “UserControl.LoadViewState Method”)
Summary
This guide covered the common causes and solutions for this .Net issue. If the problem persists, check the official documentation or system logs for additional diagnostic information.
Related Articles
- From where does ASP.Net tries to load Assembly files?
- How to: Telerik RadGrid loses it’s state of expanded and contracted groups whenever you databind
- How to: turn off custom errors for debugging mode in SharePoint 2010 application pages
- Entity Framework 4: Any way to know the plural form of an Entity Type?