जगदीश खोलिया: Is it possible to store view state in server side?

Tuesday, May 15, 2012

Is it possible to store view state in server side?

Yes, we can implement server side viewstate.

For this we have to override two virtual methods LoadPageStateFromPersistenceMedium() and SavePageStateToPersistenceMedium() of System.Web.UI.Page (on your aspx page)

However, it is not straightforward, because a user can visit a page in browser, hit the back button to a previous page and push a button (which would require the viewstate for that page). We have to handle such scenarios carefully.

Sample code:


//overriding method of Page class
protected override object LoadPageStateFromPersistenceMedium()
{
    //If server side enabled use it, otherwise use original base class implementation
    if (ConfigurationManager.AppSettings["ServerSideEnabled"].Equals("true"))
    {
        //Your implementation here.
    }
    else
    {
        return base.LoadPageStateFromPersistenceMedium();
    }
}


protected override void SavePageStateToPersistenceMedium(object state)
{
    //If server side enabled use it, otherwise use original base class implementation
    if (ConfigurationManager.AppSettings["ServerSideEnabled"].Equals("true"))
    {
        //Your implementation here.
    }
    else
    {
        base.SavePageStateToPersistenceMedium(state);
    }
}
Post a Comment