Tuesday, July 02, 2013

How to disable Viewstate in .Net

Viewstate reminds me of a monster in a horror movie that just won't stay dead - it keeps coming back, and back.  No matter how many silver bullets you pump into its chest, it keeps coming.
You can turn off viewstate by adding the attribute "EnableViewState" to your page.

 <%@ Page language="c#" EnableViewState="false" Inherits="MyAwesomeApp.table" %>

But it didn't work in my page.  The value of EnableViewState is overridden by the MasterPage's value.
You can also change "EnableViewState" in web.configs with

<pages enableViewState=“false” />

But depending on which subdirectory the web.config is in, it may be overridden by something else.
The way I finally killed viewstate was by explicitly setting  the "EnableViewState" variable on the page:


private void Page_Load(object sender, EventArgs e)
  {
        this.EnableViewState = false;
        ...
   }

Then it stayed dead. 
Hmmm.... hey wait ... is that a heartbeat I'm hearing?

No comments: