Debugging viewstate size in .Net

I’ve been debugging a problem in a web application lately that involves Safari on iOS mangling the .Net viewstate parameter if it gets very large. Part of figuring out what was going on was to see what the ViewState actually was on each round trip and see what user actions were causing it to grow the most and when. I found that putting server-side code in place to spit out the results was not reliable, for a variety of reasons. The simple solution was to just add one line of javascript to broadcast to the developer what the new size really is. Surfing around the site, I was now able to easily see what made it jump from 3k to 40k. Really, you should have something like this in place BEFORE you try applying techniques to trim your viewstate down to a manageable size. Otherwise, you won’t have a good way to measure how much you are accomplishing.

<script type="text/javascript">
    alert('The Viewstate is now '
    + $('#__VIEWSTATE').val().length + ' bytes.');
</script>

Or, if you want a detailed breakdown of what’s in the viewstate on each round trip, instead add:

Trace="true"

To the Page directive at the very top of your .aspx.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>