Various Ways to Disable ViewState in ASP.Net 4.0


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:



        <pages enableViewState="false" />




    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.




          <pages enableViewState="false" />


        <location path="ShowPage.aspx">


            <pages enableViewState="true" />




    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 >


           <pages enableViewState="true" />           




    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.



    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" "">

    <html xmlns="">

    <head runat="server">




        <form id="form1" runat="server">


           <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"






    Up Next
      Ebook Download
      View all
      View all