16

I need to completely disable ViewState for a .aspx page inside my web application. I have gone through different blogs and what I understand is that we must set <%@ Page EnableViewState="false" ...%>, but this is not working.

Is this enough to disable ViewState for all the controls inside the page? Or should I make any additional modifications? If so, please specify them. I don't want the ViewState enabled for even a single control inside the .aspx page

TylerH
  • 20,799
  • 66
  • 75
  • 101
Mridul Raj
  • 1,001
  • 4
  • 19
  • 46

3 Answers3

18

Disabling View State

  1. Machine Level - Disabling view state at machine level in machine.config, will disable ViewState of all the applications on the web server.
<Machine.config>
   <system.web>
      <pages enableViewState="false" />
   </system.web>
</Machine.config>
  1. Application Level - You can disable ViewState for all pages in /web.config file.
<configuration>
   <system.web>
      <pages enableViewState="false" />
   </system.web>
</configuration>
  1. Page Level - Disabling view state for a specific aspx file at the top.

<%@ Page Language="C#" .. EnableViewState="false" .. %>

  1. Control Level - You can disable ViewState for a specific control.

<asp:TextBox EnableViewState="false" ID="Name" runat="server"></asp:TextBox>

aaronP
  • 115
  • 4
Johnny
  • 303
  • 3
  • 7
  • Can we mix and match this? For example, can I disable View State at the Page Level and then enable it for a few controls at the control level? – Adam Apr 05 '17 at 15:35
  • http://stackoverflow.com/questions/9366469/disable-asp-net-viewstate-for-some-controls-but-not-all – Johnny Apr 07 '17 at 18:12
14

I think the quotes should be:

EnableViewState="false"

Apart from that, if you are still seeing the hidden fields then they are used by ASP.Net. You may see:

Page.EnableViewState Property

Even if EnableViewState is false, the page might contain a hidden view state field that is used by ASP.NET to detect a postback.

Habib
  • 219,104
  • 29
  • 407
  • 436
  • Thanks for reply Habib. Im actually using EnableViewState="false". It was a spelling mistake in my post – Mridul Raj Oct 03 '12 at 06:12
  • 1
    @MridulRaj, are you still seeing your controls maintaining view state ? because `EnableViewStat = "false"` will disable the view state for the controls, if you are looking at the page source then the hidden fields might be the one used by ASP.net to detect post back – Habib Oct 03 '12 at 06:14
3

If you truly don't need postback, you can remove the form element from your page, this will remove the viewstate entirely.

albattran
  • 1,887
  • 1
  • 12
  • 16
  • 2
    Or you just remove the runat="server" attribute from the Form element. – user1176058 Apr 02 '15 at 14:06
  • 1
    Fair enough, but why have a form if you are not doing a postback? – albattran Apr 11 '15 at 21:49
  • I removed runat="server" as user1176058 suggested and that disabled viewstate and any ASP.NET hidden fields. The form still posts back to the page but the IsPostback flag is not set. The page code has to detect the postback manually. – Joe B Apr 25 '16 at 15:20