Losing state of WebForm User Control
asp.net, webforms
Solution
Probably the most common way to maintain state is to use ViewState rather than private instance fields. For example, if your UserControl contains a property `Text`, you can define it as follows:
public string Text
{
get { return (string) ViewState["Text"]; }
set { ViewState["Text"] = value; }
}
You can also delegate a property to a child control. In this case the child control will maintain ViewState for you (provided `EnableViewState` is `true`). For example:
public string Text
{
get { return MyTextBox.Text; }
set { MyTextBox.Text = value; }
}
Google for `ViewState` for more info. There are pitfalls and it's a good idea to understand it thoroughly.
Problem
First of all, I have no experiences with ASP.NET WebForms. However, I inherited a very old WebForm application where I have to do maintenance from time to time. This is such a situation: There is a user control that has private instance variables. The problem is, that the values of these instance variables get lost, most likely after page reload. What I found out so far is, that it seems that the control class gets recreated frequently. What would be helpful, would be answers to the following questions: - Do WebForm User Controls do maintain a state? - If yes, how is it done in general (pointers to online resources for details appreciated) - If no, can it be implemented somehow? Any samples?