WhoIsKB - Kevin Blakeley

Public WebLog WhoIsKb() { return "Random experiences with .Net" ; }

<October 2008>
SuMoTuWeThFrSa
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678


Navigation

Tools I Love

Subscriptions

Post Categories



Control your view state

I just came across a post on the asp.net forums about wanting to control how view state is saved with a page. As we all know by default view state is rendered as a hidden element on the page, but that can easily be changed and you can set your pages up so that viewstate is saved to the cache, session, or the database.

The first step I would suggest is to create a base page for your aspx pages and override two methods, LoadPageStateFromPersistenceMedium and SavePageStateToPersistenceMedium. As you can tell by their names, these are the two methods responsible for determining how view state is actually handled in the page.

The following is what I put together for the SavePageStateToPersistenceMedium method. This method will take the viewstate object that is passed in and run it through a special formatter to serialize the object. According to MSDN, the LosFormatter object is designed for highly compact ASCII format serialization and serializes the view state for a WebForm page. Once the object has been serialized, I then save the viewstate to a session variable. I used a session variable for demonstration purposes only, but once the object has been serialized, it can be saved to session, cache, or a database. Here is the code for the SavePageStateToPersistenceMedium method:

protected override void SavePageStateToPersistenceMedium(object viewState)
{
     // Saftey check to ensure we have a valid object
    
if( viewState==null) return;
     
     // Instantiate the formatter used by the framework to serialize viewstate
    
LosFormatter formatter = new LosFormatter();
     // We need a stream writer to write the viewstate text to
    
StringWriter writer = new StringWriter();
     try
     {
          // Serialize our view state to the string writer
         
formatter.Serialize( writer, viewState );
          // Save the view state to session state
         
Session["ViewState"] = writer.ToString();
     }
     catch
     {
          throw new HttpException("Invalid view state"); 
     }
     finally
     {
          // Clean up
         
writer.Close();
     }
}

The next step is to override the LoadPageStateFromPersistenceMedium and load the viewstate data from the same location it was saved to in the SavePageStateToPersistenceMedium method. This is where we take the data out of our session object and then deserialie the data. Here is the code for the LoadPageStateFromPersistenceMedium method:

protected override object LoadPageStateFromPersistenceMedium()
{
     object viewStateObj = null;
     // Instantiate the formatter used by the framework to serialize viewstate
     LosFormatter formatter = new LosFormatter();
     // Grab our viewstate from session
     object sessionViewState = Session["ViewState"];
     try
    
{
          // Deserialize the object
          viewStateObj = formatter.Deserialize(sessionViewState.ToString());
     }
     catch
    
{
          throw new HttpException("Invalid view state");
     }
     return viewStateObj;
}

Once you implement this code, you will notice that a hidden viewstate field is still dumped out into your page, but its empty.

posted on Thursday, June 23, 2005 1:28 PM by whoiskb





Powered by Dot Net Junkies, by Telligent Systems