Saturday, February 19, 2011

How to get rid of View State completely in .NET

How can I get rid of:

<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="..."/>

Completely !

From stackoverflow
  • I think you can disable it in machine.config :

    < Pages enableViewState="false"/>

    That should disable viewstate for all pages.

  • At the control level, EnableViewState="false".

    At the page level, EnableViewState=false in the Page directive.

    At the applicaiton level add this to .config, < pages enableViewState="false" >

  • You need to add the EnableViewState="false" to the @Page directive in the Default.aspx file.

    <%@ Page Language="C#" AutoEventWireup="true"
    Codebehind="Default.aspx.cs" Inherits="Sample._Default"
    EnableViewState="false" %>
    

    Then, add the following code to the Default.aspx.cs file. This removes the hidden field from the generated HTML.

        #region Disable ViewState
        protected override void SavePageStateToPersistenceMedium(object state)
        {
        }
        protected override object LoadPageStateFromPersistenceMedium()
        {
            return null;
        }
        #endregion
    
  • #region Disable ViewState
    protected override void SavePageStateToPersistenceMedium(object state)
    {
    }
    protected override object LoadPageStateFromPersistenceMedium()
    {
        return null;
    }
    #endregion
    

    This is awesome.However, just to let everyone know it's still rendering an empty viewstate hidden field

    <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="" />
    

0 comments:

Post a Comment