I'll show you how you can quickly enable ViewState compression and show you the effects it has on overall ViewState size and server performance.
How do I enable Viewstate compression?
Enabling ViewState compression with the RadCompression module really couldn't be easier. It requires no changes to your code, no changes to your markup, and not even a change to your web.config (assuming you've already added the required HttpModule references). The only thing you need to do is add a new Browser file to your web project (or modify your Browser file if you're already using one). To add a new Browser file, follow these simple steps:
- Right-click on your web project, select "Add." > "Add ASP.NET Folder." > "App_Browsers"
- Right-click in your new App_Browsers folder, select "Add." > "Add New Item." > "Browser File"
- The new browser file will have some default values, feel free to delete everything and then add this code:
<browsers>
<browser refID="Default">
<controlAdapters>
<adapter controlType="System.Web.UI.Page"
adapterType="Telerik.Web.UI.RadHiddenFieldPageStateCompression" />
<!--Uncomment the following line to use
SessionStateCompression and remove
ViewState from the page-->
<!--<adapter controlType="System.Web.UI.Page"
adapterType="Telerik.Web.UI.RadSessionPageStateCompression" />-->
</controlAdapters>
</browser>
</browsers>
And that's it!. Run your project at this point and you'll discover that the RadCompression module is now automatically compressing your ViewState.
How much does the compression help?
ViewState, as you probably know, is just a serialized string of data. By default, it's not compressed, it's not encrypted, and it's often a bloated piece of string data that gets passed back and forth with every request to the server.
Compressing this data is an easy way to reduce your page size and reduce the number of bytes you must send and receive from the server.
To demonstrate RadCompression's effectiveness, I created a simple page with a RadGrid that loads data from the AdventureWorks Employees table. I bound my grid declaratively to an EntityDataSource and I slowly started increasing my Grid's page size- from 10 records to 240 per page. As the page size grew, obviously the ViewState grew.
What is the effect on server performance?
Many people will often say, "Sure you can compress ViewState, but doesn't that add significant processing overhead on the web server?" It is true that adding compression represents extra CPU cycles and it does have an effect on server performance. With RadCompression, though, that impact is relatively small. Internally, RadCompression uses Deflate compression to compress and decompress state, which is much faster than Gzip compression. And thanks to the optimizations of the compression APIs in .NET, the overall effect on server processing time is low.
This may be one of the easiest ways to help reduce your page size, and in turn, help improve your page performance. By simply registering the RadCompression HttpModule and adding a .browser file to your project, you can see reductions of over 70% in your ViewState size with little impact on your server performance. Clearly, as developers we should all be taking active steps to reduce our ViewState size, but if you've already done everything you can (or at least everything you have time for) and you want to squeeze a few more KB out of your page, RadCompression is ready and waiting for you in your Telerik toolbox.