SessionPageStatePersister Class
Stores ASP.NET page view state on the Web server.
Assembly: System.Web (in System.Web.dll)
ASP.NET pages can store Page state information between the inherently stateless HTTP request and response required to process and serve any Web page. This state is called "view state."
The default ASP.NET persistence mechanism is to store view state on the client using the HiddenFieldPageStatePersister class. Storing view state and data with each HTTP request and response performs well in general and is important in large Web farm scenarios because it does not matter which Web server services the request: the page state is available in the current context for the server to accurately render the page.
In scenarios where pages are served to small devices that have limited client-side resources or use a markup language that does not support a hidden field element, it is required to store view state on the server. Several ASP.NET device page adapters override the GetStatePersister method to return a SessionPageStatePersister object that stores page state on the server in the session object associated with the client.
The following code example demonstrates how you can write a PageAdapter class to return an instance of the SessionPageStatePersister class instead of the default HiddenFieldPageStatePersister class to save view state to the server-side session object.
-
AspNetHostingPermission
for operating in a hosted environment. Demand value: LinkDemand; Permission value: Minimal.
-
AspNetHostingPermission
for operating in a hosted environment. Demand value: InheritanceDemand; Permission value: Minimal.
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Whichever way you look at it, the MSDN example has a bug in it. When you set the DataKeyNames property of a GridView early in the page lifecycle, ASP.NET will call Page.PageStatePersister twice. The MSDN example would give you two separate instances, Andrew's code gives you only one (correct).
- 12/2/2009
- Alan Singfield
Andrew I don't think it is a better example as you have to insert your code into every Page derived classes.
The example provided in this article uses a PageAdapter which :
- already takes care of creating only one instance of the SessionPageStatePersister
- is automagically set for every Page derived classes in your application
All you have to do is add the App_Browsers special folder to your solution and add this SessionPageStatePersister.browser file in it:
<browsers>
<browser refID="Default">
<controlAdapters>
<adapter controlType="System.Web.UI.Page"
adapterType="Samples.AspNet.CS.MyPageAdapter" />
</controlAdapters>
</browser>
</browsers>
I'd suggest that the following C# code should be used instead of the example provided, since ControlState will be lost if SessionPageStatePersister is created multiple times.
PageStatePersister _pers;
protected override PageStatePersister PageStatePersister
{
get
{
if (_pers == null)
_pers = new SessionPageStatePersister(this);
return _pers;
}
}
- 5/23/2008
- Andrew Cupper