One of the biggest drawbacks of using ASP.NET is the way it can really bloat a page size with ViewState. That is because http and the web in general are stateless by nature, but when creating applications developers often need to track what changed on a form ... hence ViewState was introduced to essentially let the development code as if the web was stateful. Although some left-wing purist might argue, my more pragmatic nature causes me to think this was a good thing overall because of the dramatic increase in productivity it offers. However, the downside was bloated pages to the client ... but now ASP.NET 4.0 offers a better way to control that.
Pre 4.0, ASP.NET offered the EnableViewState property which you could put on a page or control. If you set that value to false, it disabled all viewstate for that control and any child controls. The problem was you didn't have the ability to override that behavior in any of the child controls. So this was more of a system where it had to be enabled at a high level, but then disabled in an opt-out fashion where you didn't really need it. The result was that people just didn't opt-out. That isn't because they were lazy (well, not completely) ... its just that pattern doesn't encourage good practices.
ASP.NET 4.0 includes a new ViewStateMode property that is completely different than EnableViewState. It allows you to set the overall viewstate for a site off by default, but that can be overridden at any level below that. This creates an opt-in pattern. When I first tried to confige this setup, I ran into a few problems because I was trying to mix EnableViewState with ViewStateMode. EnableViewState always trumps the ViewStateMode setting. I think it is much simpler to not mix the two and just avoid the old school EnableViewState property all together, and solely rely on ViewStateMode to control your ViewState.
The ViewState Opt-In Pattern Using ViewStateMode
- Set the site's masterpage(s) to have a ViewStateMode setting of "Disabled". Unfortunately you can't set that property at the page level in the web.config, but disabling at the master page level should have a similar site-wide effect.
<%@ Master Language="C#" ViewStateMode="Disabled" ...
You could optionally set the ViewStateMode="Disabled" on one or more of the content placeholders in your master page:
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" ViewStateMode="Disabled" ...
- Then, when you need viewstate on a certain page or control ... just set ViewStateMode="Enabled" at that location. It is usually very obvious when you need this when you are developing the site from scratch, but this type of pattern can be more difficult to retro-fit into an existing app. Essentially the things that typically need ViewState are controls that you will need to access the values of on a PostBack to the server (e.g. textboxes, drop downs, checkboxes, etc).
One tricky thing I have ran into is that you can't set the ViewStateMode at the page level in the Page directive like this (at least when using master pages):
<%@ Page Title="Dynamic Schedule" ViewStateMode="Enabled" ... // WON'T WORK
Instead you should preferrably set it for the individual controls that need it that way only the ViewState data that is required would be sent to/from the client, and the page wouldn't be bloated with unnecessary ViewState data.
<asp:CheckBoxList ID="cblProducts" ViewStateMode="Enabled" ...
If you have many controls that you need to enable ViewState on, and you didn't want to have to set the property on all of them ... you could optionally just enable viewstate for an entire content placeholder section like this:
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" ViewStateMode="Enabled" ...
Here are a few more helpful resources on the new ViewStateMode functionality:
http://msdn.microsoft.com/en-us/library/system.web.ui.viewstatemode.aspx
http://weblogs.asp.net/sreejukg/archive/2010/04/06/viewstatemode-in-asp-net-4-0.aspx
http://www.mostlylucid.net/archive/2009/01/28/1312.aspx