With or Without ViewState

With or Without ViewState

29.08.2010

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.
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,

Promote your business or products in our podcasts

We produce two podcasts: Burzcast and micON. Promote your business or products in our text or video content as well. Slots are limited to one advertiser per week!

Articole Burzcast

Postări din arhivă

Revolutionizing Industry: How AI is Transforming the Industrial Internet of Things

The Industrial Internet of Things (IIoT) revolutionizes how companies operate, allowing them to optimize processes, improve efficiency, and reduce costs. One of the key technologies driving this transformation is Artificial Intelligence (AI).

Search Engine Optimization, Search Engine Marketing, and Copywriting Services are Dead. Get Over It!

SEO and SEM, as well as copywriting as we know it, are becoming less effective and are likely to become obsolete shortly.

#BlackLivesMetter

I am sad and it is a shame to see that, the country that prides itself on being the land of all possibilities and a fair country has come down to this.

A New Chapter for Burzcast

The transition to the new brand is almost done. As we are closing the doors to an old chapter in our business, we are opening them to a new one.

Why You Should Never Start a Podcast

The other day I had an interesting conversation with an old friend who was quite keen to tell me that I'm spending my money and time in the wrong way by doing a weekly podcast.

The New Burzcast Website

We have decided to build this brand in a way in which it can prove beneficial for the whole company.