Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
Control Class
 EnableViewState Property
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
Control..::.EnableViewState Property

Updated: April 2009

Gets or sets a value indicating whether the server control persists its view state, and the view state of any child controls it contains, to the requesting client.

Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)
Visual Basic (Declaration)
<ThemeableAttribute(False)> _
Public Overridable Property EnableViewState As Boolean
Visual Basic (Usage)
Dim instance As Control
Dim value As Boolean

value = instance.EnableViewState

instance.EnableViewState = value
C#
[ThemeableAttribute(false)]
public virtual bool EnableViewState { get; set; }
Visual C++
[ThemeableAttribute(false)]
public:
virtual property bool EnableViewState {
    bool get ();
    void set (bool value);
}
JScript
public function get EnableViewState () : boolean
public function set EnableViewState (value : boolean)

Property Value

Type: System..::.Boolean
true if the server control maintains its view state; otherwise false. The default is true.

You must enable view state for the server control to maintain its state across HTTP requests.

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 uses an instance of the StateBag class to store the property values. The values are then passed as a variable to a hidden field when subsequent requests are processed. For more information about view state, see ASP.NET View State Overview.

There are times when it is appropriate to disable view state, particularly to improve application performance. For example, if you are loading a database request into a server control, set this property to false. If you do not, processor time will be wasted loading view state into the server control that will only be overridden by the database query. If EnableViewState is false, you can use the control state to persist property information that is specific to a control and cannot be turned off like the view state property. For more information on the difference between control state and view state, see Control State vs. View State Example.

For information about how to enable or disable view state declaratively for an ASP.NET page, see @ Page.

The following example sets the EnableViewState property to false.

Visual Basic
Sub Page_Load(sender As Object, e As System.EventArgs)
   DataBind()
   ' Set EnableViewState to false to disable saving of view state 
   ' information.
   myControl.EnableViewState = False
   If Not IsPostBack Then
      display.Enabled = False
   End If 
End Sub
C#
void Page_Load(object sender, System.EventArgs e)
{
      DataBind();
      // Set EnableViewState to false to disable saving of view state 
      // information.
      myControl.EnableViewState = false;
      if (!IsPostBack)
         display.Enabled = false;

}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

Date

History

Reason

April 2009

Removed link to out-of-date What's New topics.

Content bug fix.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2010 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker