Name

EnableViewState

Synopsis

Boolean = Page.EnableViewState
Page.EnableViewState = Boolean

Returns or sets a Boolean value that indicates whether the Page maintains its view state and that of server controls it contains. The default value of this property is True, which means that the page maintains its view state.

Parameters

Boolean

A Boolean value that indicates whether the page maintains its view state.

Example

The following code example sets EnableViewState to False using the EnableViewState attribute of the @ Page directive and displays its value on the page:

<%@ Page Language="vb" EnableViewState="True" %>
<html>
   <head>
      <title></title>
      <script runat="server">
         Sub Page_Load(  )
            If Page.EnableViewState = True Then
               Message.Text = "ViewState is enabled."
            Else
               Message.Text = "ViewState is disabled."
            End If
         End Sub
      </script>
   </head>
<body>
   <form runat="server">
      <asp:label id="Message" runat="server"/>
   </form>
</body>
</html>

Notes

The EnableViewState property can also be specified using the EnableViewState attribute of the @ Page directive, as shown in the example.

Examining a page’s HTML source using a browser’s View Source feature shows the effect of the EnableViewState property. If the EnableViewState property is set to False, the source will look similar to:

<input type="hidden" name="_  _VIEWSTATE" 
       value="dDwxMDA3MzE2MzEyOzs+" />

If the EnableViewState property is set to True, the source will look similar to:

<input type="hidden" name="_ _VIEWSTATE" value="dDwxMDA3MzE2MzEyO3Q8O2w8aTwxPjs+O2w8dDw7bDxpPDM+Oz47bDx0PHA8cDxsPF ...

Get ASP.NET in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.