6

How can I get rid of:

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

Completely !

Mat Nadrofsky
  • 8,289
  • 8
  • 49
  • 73
Boris Smirnov
  • 1,217
  • 4
  • 15
  • 27
  • It's obvious that you want to get rid of viewstate altogether but, if you wanted just to optimise it, rather than turning it off, you might find my answer to another question helpful: http://stackoverflow.com/a/3865762/205245 – Owen Blacker Sep 06 '12 at 13:41

4 Answers4

11

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
Julio César
  • 12,790
  • 10
  • 38
  • 45
  • As answered below, it is easier to disable view state on the web.config level (or even machine.config level). – Monsignor Jul 22 '11 at 06:27
  • I wonder why that does not work for me. Still have ViewState in the GET requests. – ajeh Jun 28 '16 at 18:38
  • I did that and viewstate is still there. Any element that has `runat=server` also has viewstate disabled, viewstate is disabled in `web.config`, but the damn thing still there! – ajeh Jul 29 '16 at 19:57
5

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

n8wrl
  • 19,439
  • 4
  • 63
  • 103
4

I think you can disable it in machine.config :

< Pages enableViewState="false"/>

That should disable viewstate for all pages.

dub
  • 1,396
  • 12
  • 22
1
#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="" />
ronaldwidha
  • 1,345
  • 2
  • 12
  • 24