My friend was telling me about how evil ViewState can be when dealing with ASP pages. As I’m new to the .net version of ASP pages I went investigating a little further. Taking a look at my sample two posts ago. Sure enough I’m guilty as charged, it turns out that the state of your controls in your ASP pages is automatically put into the ViewState bucket. In other words if you have large controls with a lot of data such as grids or lists your ViewState will include all of that data held by the control. With that said all of the data will travel between your server and the client on all round trips. It is a big waste of bandwidth.
At first I started up Fiddler which allows you to see the real http data transferred. If you do not yet have Fiddler in your toolbox you should check it out, it’s a great tool. However it doesn’t allow you to examine the ViewState in detail as the tool is more generic.
I did the google and came up on another tool, ViewState Helper Viewer it does exactly what I needed. It shows you the size of your ViewState and further you can drill down on it with a double click which will break the ViewState down into the variables that it’s holding, good job by the author Jon Tackabury. Jon has actually a couple of other impressive tools. As I’m writing this I realize that Jon already wrote ViewState plugin for Fiddler, which is great, I have to check that out.
So let’s take a closer look at the webpage, as seen in the viewer
As you can see at first on the highlighted row, the size of the ViewState is substantial. It is about 31% of the whole page, 15K of worthless data traveling to and from the client on each roundtrip, not good. In order to cut down on the size, simply add an attribute to your control in the html code, EnableViewState=”false”. This will instruct ASP .net not to include the state / data of the control in the ViewState bucket. As I applied that to the grid control you can see the next line above shows much better results than before. Then I further applied it to other controls on the page and now we are down to 456 bytes from 15,184.
If you want to read in depth analyzes of the ViewState and how it works in .net you might want to wander over here. For most pages you might just have to set the EnableViewState attribute on your controls, but for more advanced cases you might have to drill down further.
Sean thanks for being the devils advocate.