ViewState Chunking in ASP.NET 2.0 (maxPageStateFieldLength)

I am currently teaching an ASP.NET 2.0 course in Quebec city. I like it because each time I give a training I learn something (and so do my trainees I hope…). This time I learnt something new about the ViewState.

When ViewState in your page become very large it can be a problem as some firewalls and proxies will prevent access to pages containing such huge ViewState sizes. For this purpose ASP.NET 2.0 introduces the ViewState Chunking mechanism. But the first thing I would say is if your ViewState is that big ask yourself why, and the way to optimize (maybe disabling?) it.

So ASP.NET 2.0 enables to split the ViewState's single hidden field into several using the MaxPageStateFieldLength property in the web.config section. This indicates the maximum bytes size allowed for one viewstate hidden field. If the real size exceeds the value then viewstate is splitted in multiple fields. By default, the attribute is set to -1 which means that no maximum size is defined.

Sample ViewState before: 
<input type=“hidden” name=“__VIEWSTATE” id=“__VIEWSTATE”

Then set in web.config:
<pages maxPageStateFieldLength=“40”>

Sample ViewState After :
<input type=“hidden” name=“__VIEWSTATEFIELDCOUNT” id=“__VIEWSTATEFIELDCOUNT”
value=“3” />
<input type=“hidden” name=“__VIEWSTATE”
id=“__VIEWSTATE” value=“/wEPDwUKLTk2Njk3OTQxNg9kFgICAw9kFgICCQ88” />
<input type=“hidden” name=“__VIEWSTATE1”
id=“__VIEWSTATE1” value=“KwANAGQYAQUJR3JpZFZpZXcxD2dk4sjERFfnDXV/” />
<input type=“hidden” name=“__VIEWSTATE2”
id=“__VIEWSTATE2” value=“hMFGAL10HQUnZbk=” />

Please note THIS IS NOT a way to optimize ViewState size!! This is a way to fix a possible technical problem with huge ViewState size and firewall.