Saturday, May 9, 2015
No Comments Yet!!!
Disable or Remove ViewState in ASP.Net 4.0 ViewState Disable ASP.NET Remove ViewState
First we know what is ViewState and what is the purpose of ASP.NET ViewSate.
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.View state is the method that the ASP.NET page framework uses to preserve page and control values between round trips. When the HTML markup for the page is rendered, the current state of the page and values that must be retained during postback are serialized into base64-encoded strings.
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
1. 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" />
2. 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="Routing.aspx.cs" Inherits="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;
3, 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:
<pages enableViewState="false" />
4. 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:
<pages enableViewState="true" />
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.
Hope you have understand various ways to disable ViewState and also define the ViewStateMode Property in ASP.Net.