ASP .NET Postback Performance – Part 2

In my previous post I described a performance issue I ran across due to a large amount of ViewState.  Now I will discuss some possible solutions.

To fix this issue I decided to store the ViewState in the Session, by overriding a PageStatePersister property to return SessionPageStatePersistor. The default page state persister is a HiddenFieldStatePersister, which stores ViewState as a hidden Base64-encoded string on the page.  To read more about the PageStatePersister, see the following MSDN documentation.

Code Snippet
  1. Protected Overrides ReadOnly Property PageStatePersister() As PageStatePersister
  2.     Get
  3.         Return New SessionPageStatePersister(Me)
  4.     End Get
  5. End Property

This functionality can be implemented as needed for each page.  I implemented above code only for the few pages that were performing poorly and got the following results:

    • From 143K per postback to 13k per postback
    • From 188K per postback to 2k per postback
    • From 700k per postback to 1k per postback
    • From 244K per postback to 1k per postback

To implement this functionality application wide, override PageStatePersister in the base class for all your pages.  This does place a larger load on the web server, so make sure that this is a right solution for you.

As a long term fix, I plan on going through the screens and disabling ViewState for controls that don’t need it. Since majority of controls do not need ViewState enabled, this should minimize the size of the ViewState.