Tuesday, 24 February 2009

View State Chunking

We all know that in ASP.NET, View State is the solution for persisting changes done to the Web Form across different page requests. It does this by using HTML hidden field and ASP.NET's integrated serialisation and de-serialisation mechanism. I am certain that Viewstate mechanism is a boon for those who have come from a background of classic ASP development.

Lets not put too much time in understanding what View State is and why its here. Lets see what View State chunking is..

Though a hidden field can store unlimited information, some proxy servers and firewalls restrict the pages to pass through if data in the hidden field is greater than certain size. For example, you have a gridview on your form and its showing massive amount of data with EnableViewState is set to true, then its likely to persist heavy information in hidden field. Such pages may suffer restrictions on certain proxy servers or firewalls. In such situation View State Chunking mechanism comes handy. It automatically divides the view state data across multiple hidden fields and makes sure that no hidden field surpasses the defined size.

We need to visit web.config file for achieving this.

<configuration>
<system.web>
<pages maxpagestatefieldlength = "2048">
</system.web>
</configuration>


Set the maxPageStateFieldLength attribute of the element in web.config file. The size is specified in bytes. In above example, the maximum amount of viewstate data allowed to be stored in single hidden field is 2KB. If view state information exceeds 2 KB then you can check in the HTML source that multiple view state hidden fields are emitted in the page.

As a good practice, try to keep as less data as possible in viewstate for better performance. So now we understood that View State chunking is a technique for avoiding problems that may created by certain proxies and firewalls.

I hope you will find this information helpful!