How to get the value of built, encoded ViewState?

See this blog post where the author describes a method for overriding the default behavior for generating the ViewState and instead shows how to save it on the server Session object.

In ASP.NET 2.0, ViewState is saved by a descendant of PageStatePersister class. This class is an abstract class for saving and loading ViewsState and there are two implemented descendants of this class in .Net Framework, named HiddenFieldPageStatePersister and SessionPageStatePersister. By default HiddenFieldPageStatePersister is used to save/load ViewState information, but we can easily get the SessionPageStatePersister to work and save ViewState in Session object.

Although I did not test his code, it seems to show exactly what you want: a way to gain access to ViewState code while still on the server, before postback.


Rex, I suspect a good place to start looking is solutions that compress the ViewState -- they're grabbing ViewState on the server before it's sent down to the client and gzipping it. That's exactly where you want to be.

  • Scott Hanselman on ViewState Compression (2005)
  • ViewState Compression with System.IO.Compression (2007)

Tags:

C#

Asp.Net