Wednesday, April 16, 2008

SessionPageStatePersister Class

SessionPageStatePersister Class

Stores ASP.NET page view state on the Web server.

Namespace: System.Web.UI
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.

Example

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.

namespace Samples.AspNet.CS {
 
    using System.Web.UI;
 
    public class MyPageAdapter : System.Web.UI.Adapters.PageAdapter {
 
        public override PageStatePersister GetStatePersister() {
            return new SessionPageStatePersister(Page);
        }
    }
}

Main Benefit which I found is that

When you look at the rendered HTML for the page, you see a large hidden field for carrying the ViewState.

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  >
</pre><pre><span style="">   </span>Untitled Page</pre><pre>
    
value="/wEPDwUKMTQ0MDQzNjk2Ng9kFgICBA9kFgICAQ88KwANAgAPFgYeC18hRGF0YUJv
dW5kZx4JUGFnZUNvdW50AgEeC18hSXRlbUNvdW50AhRkDBQrAAEWBh4EVHlwZRkrAh4ETmF
tZQUESXRlbR4JRGF0YUZpZWxkBQEhFgJmD2QWKgIBD2QWAmYPDxYCHgRUZXh0BQEwZGQCAg
9kFgJmDw8WAh8GBQExZGQCAw9kFgJmDw8WAh8GBQEyZGQCBA9kFgJmDw8WAh8GBQEzZGQCB
Q9kFgJmDw8WAh8GBQE0ZGQCBg9kFgJmDw8WAh8GBQE1ZGQCBw9kFgJmDw8WAh8GBQE2ZGQC
CA9kFgJmDw8WAh8GBQE3ZGQCCQ9kFgJmDw8WAh8GBQE4ZGQCCg9kFgJmDw8WAh8GBQE5ZGQ
CCw9kFgJmDw8WAh8GBQIxMGRkAgwPZBYCZg8PFgIfBgUCMTFkZAIND2QWAmYPDxYCHwYFAj
EyZGQCDg9kFgJmDw8WAh8GBQIxM2RkAg8PZBYCZg8PFgIfBgUCMTRkZAIQD2QWAmYPDxYCH
wYFAjE1ZGQCEQ9kFgJmDw8WAh8GBQIxNmRkAhIPZBYCZg8PFgIfBgUCMTdkZAITD2QWAmYP
DxYCHwYFAjE4ZGQCFA9kFgJmDw8WAh8GBQIxOWRkAhUPDxYCHgdWaXNpYmxlaGRkGAEFCUd
yaWRWaWV3MQ9nZMhHZ3iQZp62S8IR8fTJ5ZL42ira" />

when we add an override the PageStatePersister property and use the built-in SessionPageStatePersister, the behavior of the page remains the same, but the storage used for the bulk of the state data is shifted from the hidden field to session state.

protected override PageStatePersister PageStatePersister
    {
        get
        {
            return new SessionPageStatePersister(this);
        }
    }
 

Notice in the source of the page that the hidden field value is much smaller, but not gone entirely. ASP.NET will still carry some minimal set of data in the page output.

value="/wEPaA8FDzhjNzkyNTMzNjE1YWEyNxgBBQlHcmlkVmlldzEPZ2QZw
44JLJFcglwRl9TiNliE82yAuQ==" />

Cheers,

Sid.

No comments: