# Wednesday, April 11, 2007
« NAnt p4edit - must refer to client X | Main | Spring.NET and Singleton Madness »
There may be a time you need access to the current executing ASP.NET handler's (page) view state without having a reference to the executing page.  I did today while creating a view state adapter that is injected into my page controllers.  I swapped out the Session adapter for this new ViewState adapter in my Spring.NET config.

Here's the code where I grab a view state reference in the adapter:

return ((PageBase)System.Web.HttpContext.Current.Handler).PageViewState;

Basically I grab the current handler and cast it to PageBase type.  In my application all pages inherit from PageBase, and PageBase exposes a public PageViewState property.  This allows me to grab the viewstate without having a direct reference to the page instance.  Some of this extra work is required because the System.Web.UI.Page ViewState property is not public, but protected.

Comments are closed.