Enabling View State for Individual Controls
By default, view state is enabled for the page, with the result that each control on the page potentially stores view state even if it is not required for the application. View state data is included in the markup that a page generates and increases the amount of time it takes to send a page to the client and post it back.
Storing more view state than is necessary can cause significant performance degradation. In earlier versions of ASP.NET, developers could disable view state for individual controls in order to reduce page size, but had to do so explicitly for individual controls. In ASP.NET 4, Web server controls include a ViewStateMode property that lets you disable view state by default and then enable it only for the controls that require it in the page.
The ViewStateMode property takes an enumeration that has three values: Enabled, Disabled, and Inherit. Enabled enables view state for that control and for any child controls that are set to Inherit or that have nothing set. Disabled disables view state, and Inherit specifies that the control uses the ViewStateMode setting from the parent control.
The following example shows how the ViewStateMode property works. The markup and code for the controls in the following page includes values for the ViewStateMode property:
<form id="form1" runat="server"> <script runat="server"> protected override void OnLoad(EventArgs e) { if (!IsPostBack) { label1.Text = label2.Text = "[DynamicValue]"; } base.OnLoad(e); } </script> <asp:PlaceHolder ID="PlaceHolder1" runat="server" ViewStateMode="Disabled"> Disabled: <asp:Label ID="label1" runat="server" Text="[DeclaredValue]" /><br /> <asp:PlaceHolder ID="PlaceHolder2" runat="server" ViewStateMode="Enabled"> Enabled: <asp:Label ID="label2" runat="server" Text="[DeclaredValue]" /> </asp:PlaceHolder> </asp:PlaceHolder> <hr /> <asp:button ID="Button1" runat="server" Text="Postback" /> <%-- Further markup here --%>
As you can see, the code disables view state for the PlaceHolder1 control. The child label1 control inherits this property value (Inherit is the default value for ViewStateMode for controls.) and therefore saves no view state. In the PlaceHolder2 control, ViewStateMode is set to Enabled, so label2 inherits this property and saves view state. When the page is first loaded, the Text property of both Label controls is set to the string “*DynamicValue+”.
The effect of these settings is that when the page loads the first time, the following output is displayed in the browser:
Disabled: [DynamicValue]
Enabled: [DynamicValue]
After a postback, however, the following output is displayed:
Disabled: [DeclaredValue]
Enabled: [DynamicValue]
The label1 control (whose ViewStateMode value is set to Disabled) has not preserved the value that it was set to in code. However, the label2 control (whose ViewStateMode value is set to Enabled) has preserved its state.
You can also set ViewStateMode in the @ Page directive, as in the following example:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" ViewStateMode="Disabled" %>
The Page class is just another control; it acts as the parent control for all the other controls in the page. The default value of ViewStateMode is Enabled for instances of Page. Because controls default to Inherit, controls will inherit the Enabled property value unless you set ViewStateMode at page or control level.
The value of the ViewStateMode property determines if view state is maintained only if the EnableViewState property is set to true. If the EnableViewState property is set to false, view state will not be maintained even if ViewStateMode is set to Enabled.
A good use for this feature is with ContentPlaceHolder controls in master pages, where you can set ViewStateMode to Disabled for the master page and then enable it individually for ContentPlaceHolder controls that in turn contain controls that require view state.