Sunday, September 13, 2009

Compressing ViewState in ASP.NET

Since the days of ASP.NET 1.1 ViewState has been a sore point for many developers. ASP.NET has allowed many windows forms programmers to transition to web. They have included design time support and drag and drop functionality in ASP.NET that many junior and mid level developers use. The big downside of ASP.NET is the amount of ViewState that goes back and forth to the web server on EVERY postback.

One way is to rewrite your site using AJAX. And please not MS Ajax with their UpdatePanels. This may look like AJAX but you are still posting the entire ViewState to the server. Using AJAX will unfortunately require you to rewrite a lot of your web application.

If this is not an option you can try compressing the ViewState that ASP.NET uses. I found a nice article by Scott Hanselman that describes exactly this. He even went as far to provide a base page for us to implement. Unfortunately its in VB.NET but not everyone is perfect :). The beauty of .NET is that you can just extract this base class in different assembly and reference the DLL.

By using this method you can typically compress around 50% of your ViewState. That is a huge saving.

No comments:

Post a Comment