In this article I will explain with examples, different ways to disable ViewState in ASP.Net.
ViewState can be disabled at the following levels:
1. Control Level.
2. Page Level.
3. Application Level.
This article will also explain how to completely remove the ViewState Hidden Field from the Page.
Disable ViewState at Control Level
ViewState can be easily disabled for a particular control by setting EnableViewState property to False.
<asp:TextBox ID = "txtName" runat="server" EnableViewState = "false" />
Disable ViewState at Page Level
ViewState can be disabled for the whole Page i.e. all controls on the Page by setting the EnableViewState property to False in the @Page Directive.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" EnableViewState = "false" %>
Note: Above action will disable ViewState for all controls on the Page.
Disable ViewState at Application Level
ViewState can be disabled for the whole Application i.e. all Pages by setting the enableViewState property to False in the pages section of the Web.Config file.
Note: Above action will disable ViewState for all controls on all Pages in the Application.
Completely removing ViewState Hidden Field
Even when the ViewState is disabled, still ASP.Net will insert the ViewState Hidden Field on the page and hence in order to remove the ViewState Hidden Field please refer the following article.