Razvan Burz

de Razvan Burz

With or Without ViewState

As many of the ASP .NET developers know, ViewState is a necessary evil. Can’t live with it, can’t live without it.

  The easiest way to disable ViewState is to turn it off from the web.config file of your web application. To do that, just use the following code:  
 1: <system.web>

 2: <pages enableViewState="false" />

 3: </system.web>

  or, if you have the below settings or something similar to these  

 1: <pages>

 2: <namespaces>

 3: <clear/>

 4: <add namespace="System"/>

 5: <add namespace="System.Web.Security"/>

 6: <add namespace="System.Web.UI.HtmlControls"/>

 7: </namespaces>

 8: </pages>

  just replace <pages> with <pages enableViewState=”false”> and keep the rest of the lines as they are. See below:  

 1: <pages enableViewState="false">

 2: <namespaces>

 3: <clear/>

 4: <add namespace="System"/>

 5: <!-- something more here -->

 6: </namespaces>

 7: </pages>


So What Happens Now?

  EnableViewState is True   In 75% of the cases (I am talking here about the small sites, not the quite-complex-web-applications), you shouldn’t experience any problems with the disabled ViewState. You may also do one of the following things:  
  • Disable ViewState from the MasterPage. The ViewState will be inherited.
  • Move ViewState into a session of your choise, to keep the form data or move the data and keep it intro a cookie. Don’t forget to clear the cookie data when it is no longer needed.
  • You may disable ViewState from the Properties window of each component on the ASP .NET page.
  • Disabling the ViewState from web.config only decreases the id=”__VIEWSTATE” value=”" data to something smaller.
  • Anyway, I strongly recommend using HttpCompression instead. It is safer and may keep your site errors free. 78 kb of ViewState value data is a lot for a page to load.
  • Natty Gur posted another method to disable ViewState (setting EnableViewState property to false in web.config, but also keep the data).
  • “Spend less time writing wrongheaded plumbing code to replace ViewState, and instead learn how to use it effectively and efficiently.” said Scott Hanselman.
  In the ASP .NET Forums is a discussion about moving the ViewState data into a database, but this option has its downsides as well. Opting for this you will cause a lot of load on the server’s CPU usage and lots of SQL queries if you use lots of forms. If these doesn’t affect your web application’s performance, take a look at the code, understand it and use it. Just remember that you may lose all the form component’s data from turning off ViewState, if you don’t find a workaround that you tested and are sure it works for you.   On MSDN’s article “Taking a Bite Out of ASP.NET ViewState”, you may learn more about ViewState. To understand what is stored in the ViewState value data, you may use ViewState Decoder.   If you really, really, really can’t live with ViewState and you want it moved on the server side that bad, try this tutorial and it might be something close to what you were looking for. Just remember, you can never replace the old value with some plumbing code and hope it will work as good as it does with the long value. If it is a downside for your site, maybe is time to reconsider the technology you are using for your site. You may hire Dev-Vision to migrate your site to PHP (LAMP) or JSP.      

Use HTTPCompression Instead

  I rather use HttpCompression with the Blowery HttpCompression Module instead of trying to fix ViewState’s size.  
 1: <configSections>

 2: <sectionGroup name="blowery.web">

 3: <section name="httpCompressionModule" type="
blowery.Web.HttpModules.HttpCompressionModuleSectionHandler, HttpCompressionModule"

 4: </sectionGroup>

 5: </configSections>

 6: <blowery.web>

 7: <httpCompressionModule preferredAlgorithm="gzip"  compressionLevel="high"/>

 8: </blowery.web>

 9: <system.web>

  As a downside, some versions of Internet Explorer and other old browsers may experience some problems with the HttpCompression.   So, what are you choosing: HttpCompression or ViewState (disabled)? I am looking forward to your comments.   Cheers,

Ultimele două articole
Razvan Burz

Despre Razvan Burz

Salut, sunt Răzvan Burz: programator cu o vechime de peste 15 ani în software și web development, antreprenor, tată, soț, consultant pentru companii din multiple țări, podcaster, blogger, gamer ocazional și pasionat de cafea.

Poți obține conținut audio, video și text adițional exclusiv prin Burzcast™ VIP

Fă parte din comunitatea VIP, împreună cu alte sute de români minunați!

Abonează-te la newsletter!