Control.ViewState Property
Gets a dictionary of state information that allows you to save and restore the view state of a server control across multiple requests for the same page.
[Visual Basic] Protected Overridable ReadOnly Property ViewState As StateBag [C#] protected virtual StateBag ViewState {get;} [C++] protected: __property virtual StateBag* get_ViewState(); [JScript] protected function get ViewState() : StateBag;
Property Value
An instance of the StateBag class that contains the server control's view-state information.
Remarks
A server control's view state is the accumulation of all its property values. In order to preserve these values across HTTP requests, ASP.NET server controls use this property, which is an instance of the StateBag class, to store the property values. The values are then passed as a variable to an HTML hidden input element when subsequent requests are processed. For more information about saving server control view state, see Maintaining State in a Control.
View state is enabled for all server controls by default, but there are circumstances in which you will want to disable it. For more information, see Developing High-Performance ASP.NET Applications.
For information about dictionaries and how to use them, see Grouping Data in Collections.
Example
The following examples demonstrates a Text property that stores and retrieves its value from its control's ViewState property.
[Visual Basic] ' Add property values to view state with set; ' retrieve them from view state with get. Public Property [Text] As String Get Return CStr(ViewState("Text")) End Get Set ViewState("Text") = Value End Set End Property [C#] // Add property values to view state with set; // retrieve them from view state with get. public String Text { get { return (String) ViewState["Text"]; } set { ViewState["Text"] = value; } } [C++] // Add property values to view state with set; // retrieve them from view state with get. public: __property String * get_Text() { return dynamic_cast<String*>(ViewState->Item[S"Text"]); } __property void set_Text(String * value) { ViewState->Item[S"Text"] = value; } [JScript] // Add property values to view state with set; // retrieve them from view state with get. public function get Text() : String { return String(ViewState["Text"]); } public function set Text(value : String) { ViewState["Text"] = value; }
Requirements
Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family
See Also
Control Class | Control Members | System.Web.UI Namespace | StateBag | Maintaining State in a Control