After my talk about quality in EPiServer projects, I’ve got quite a few questions and comments. A recurring question is how to minimize the ViewState. I really thought this was a thing of the past, but I was proven wrong.

In brief, ViewState persist the state of server controls between client requests on an ASP.Net web form between postbacks. Consequently it increases the size of both the response and postback request. The ViewState is stored as a hidden field in the markup like this
ViewState example

Why ViewState is bad

In EPiServer 4.6 this had a limited usage. But since EPIServer improved internal caching in CMS 5,  ViewState is pretty much a waste of bandwidth in my opinion. And with mobile websites, you should focus on limiting the size of transferred data anyway. 

My intention is not to nail anybody to the cross. But checking a few sites using the tool ViewState-Helper, I find that at e.g., 24% of the page size is ViewState data. And checking some random client cases from EPiServer World brings no peace to mind either.

One example reports a ViewState size of 130 KB which is actually 67% of the total markup. That is close to 130 KB crap in my opinion.

How to use ViewState right

Note that disabling/reducing ViewState on an existing site might cause some unexpected issues, especially when using IsPostBack-tests in code. However, ViewState handling is improved in ASP.Net 4.0, which enables other approaches. As always, doing things right the first time is always preferable whenever possible.

And obviously when using MVC with EPiServer CMS 7, the ViewState issue is non-existing. 

The recipe we use at Epinova for Forms-based solutions:

  1. Disable the global ViewState on the global <pages> section in web.config:
    <pages enableViewState="false" 
  2. Enable ViewState for EPiServer's edit and admin <location> paths:
    <location path="EPiServer">
           <pages enableViewState="true"
  3. Consider using ControlState instead of ViewSate for controls like DropDownList.
  4. Continue support for On Page Editing by overriding OnInit in your base class for template pages:
    protected override void OnInit(System.EventArgs e)
        //Activate ViewState in OPE mode
        Page.EnableViewState |= Regex.IsMatch(Request["__EVENTARGUMENT"] ?? string.Empty, "^Edit$|^Publish$|^SaveAndPublish$", RegexOptions.IgnoreCase);