ViewState
The ViewState manages the information of the current page. It is utilized by the HTML pages by ASP.NET applications to maintain the state of the web form controls. By this you can save a lot of coding by maintaining the ViewState of the objects in your Web Form.
You do not need ViewState in the following situations:
The control is repopulated on every postback. If you ignore old data, and if you repopulate the server control each time the page is refreshed then you do not need ViewState
.
When you complete a web page, review the controls in the page and consider what information is being passed in the view state and whether you really need all that information to be maintained. To optimize web page size, consider disabling view state in these cases.
The control is an input control and it changes only from user actions.
The control never changes.
By default, ViewState is enabled for all server controls. ViewState can be enabled and disabled in any of the following ways:
- Control Level
- Page Level
- Application Level
- Machine Level
To disable ViewState
To disable ViewState for a single control on a page, set the EnableViewState property of the control to false, as in the following:
<asp:DropDownList ID="DropDownList1" runat="server" EnableViewState="false" />
To disable ViewState for a page
To disable ViewState for a single page, set the EnableViewState attribute in the @ Page directive to false, as in the following:
<%@ Page Language="C#" EnableViewState="false" AutoEventWireup="true" CodeFile="URLRouting.aspx.cs" Inherits="URL_Rewriting" %>
To disable a page's View State, add the code below in the Page class of the page.
public void DisableViewState()
{
this.Init += new EventHandler(Page_Init);
}
private void Page_Init(object sender, System.EventArgs e)
{
this.EnableViewState = false;
}
To disable ViewState for a specific application
To disable ViewState for a specific application, use the following element in the Web.config file of the application:
<configuration>
<system.web>
<pages enableViewState="false" />
</system.web>
</configuration>
Important Points
Now when you use EnableViewState="true"
in the @Page
directive it will not work once you disable the ViewState in the web.config. Like this on the page:
<%@ Page Language="C#" EnableViewState="" AutoEventWireup="true" CodeFile="URLRouting.aspx.cs" Inherits="URL_Rewriting" %>
If you decide later that you need the ViewState for a specific page then you can turn it back on for just that page using a <location> element.
<configuration>
<system.web>
<pages enableViewState="false" />
</system.web>
<location path="ShowPage.aspx">
<system.web>
<pages enableViewState="true" />
</system.web>
</location>
</configuration>
To disable ViewState for all applications on a Web server
To disable ViewState for all applications on a Web server, configure the <pages> element in the Machine.config file as follows:
<Machine.config >
<system.web>
<pages enableViewState="true" />
</system.web>
</Machine.config>
ViewStateMode Property
ASP.NET 4 introduced a new ViewStateMode Property. Instead of disabling view state by default and then picking and choosing controls on the page that should use view state. The ViewStateMode property can be set at the page level in the @Page directive or for individual controls, and can accept one of the following three values:
Inherit: Causes the control to inherit the value of the ViewStateMode property from its parent (the default),
Enabled: Enables view state for the control even if the parent control has its view state disabled
Disabled: Disables view state for the control even if the parent control has its view state enabled.
Example
The example defines that the page should have view state disabled (by default) but that a particular control within the page should have view state enabled. To do this you can set the @Page directive's ViewStateMode property to Disabled and the control of interest's ViewStateMode property to Enabled. As in the following code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DetailPage.aspx.cs" ViewStateMode ="Disabled" Inherits="DetailPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblTitle" runat="server" Text="Label" Font-Bold="true"
ForeColor="blue"></asp:Label><br />
<asp:Label ID="lblauthor" runat="server" Text="Label" Font-Bold="true" ViewStateMode="Enabled"
ForeColor="blue"></asp:Label>
</div>
</form>
</body>
</html>