Maintaining State in a Control

An ASP.NET server control inherits a property named ViewState from Control that enables it to participate easily in state management. The type of ViewState is System.Web.UI.StateBag, which is a dictionary that stores name/value pairs. ViewState is persisted to a string variable by the ASP.NET page framework and sent to the client and back as a hidden variable. Upon postback, the page framework parses the input string from the hidden variable and populates the ViewState property of each control. If a control uses ViewState for property data instead of a private field, that property automatically will be persisted across round trips to the client. (If a property is not persisted in ViewState, it is good practice to return its default value on postback.)

The following code fragment shows a property that is saved in ViewState.

public String Text {
          get {
              return (String) ViewState["Text"];
          }
          set {
              ViewState["Text"] = value;
          }
       }
[Visual Basic]
Public Property Text() As String
   Get
      Return CType(ViewState("Text"), String)
   End Get
   Set
      ViewState("Text") = value
   End Set
End Property

For an example of using ViewState to store properties, see the ASP.NET QuickStart —> ASP.NET Web Forms —> Authoring Custom Controls.

Note   ViewState is generally used for persisting form data on a page across round trips. Do not use ViewState to store information such as passwords, connection strings, and file paths. For information about sharing data across pages or more persistent storage, see ASP.NET State Management.

Types That Can Be Persisted in ViewState

A type that either is serializable or has a TypeConverter defined for it can be persisted in ViewState. However, types that are only serializable are slower and generate a much larger ViewState than those that have a TypeConverter. ViewState is serialized using a limited object serialization format that is optimized for primitive types, and for String, ArrayList, and HashTable types.

ViewState and Performance

Control developers should be aware that any data in ViewState automatically makes a round trip to the client. Because the round trips contribute to a performance overhead, it is important to make judicious use of ViewState. If there are several properties that depend on common data, you can optimize performance by persisting only key elements to ViewState. A control inherits a property named EnableViewState from Control that allows consumers of the control to enable or disable the persistence of its ViewState.

Customizing State Restoration Using ViewState

To improve efficiency, or to save a custom type that cannot be stored by default in ViewState, a control can customize how property data is stored in ViewState. If a control customizes storage of property data, the control must also provide a custom implementation for restoring property values from data stored in ViewState. The base class Control provides two methods for this purpose, SaveViewState and LoadViewState. These methods have the following signatures.

protected virtual object SaveViewState();
protected virtual void LoadViewState(object savedState);
[Visual Basic]
Overridable Protected Function SaveViewState() As Object
Overridable Protected Sub LoadViewState(ByVal savedState As Object)

For examples of overriding SaveViewState and LoadViewState, see the Templated Data-Bound Control Sample.

See Also

ASP.NET State Management | Session State | Application State