Wednesday, November 28, 2012

Completely Remove Viewstate in your ASP.NET page

How to completely disable ViewState in ASP.NET
I tried to disbale Viewstate of a website by set EnableViewState Page directive to false. But when I get the Page HTML Source I found the following line:

   1:  <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"
   2:  value="/wEPDwUKMTY1NDU2MTA1MmRk7eWz8DBE7zF7MW8sS4wNndKCr5gBJPZZ/LOS1KIbBo4=" />

In order to solve this issue put the following code in the Page's Code Behind Class:

   1:  protected override void SavePageStateToPersistenceMedium(object viewState)
   2:  {
   3:   
   4:  }
   5:   
   6:  protected override object LoadPageStateFromPersistenceMedium()
   7:  {
   8:     return null;
   9:  }