Amazon.com Widgets All posts tagged 'viewstate'

WilliaBlog.Net

I dream in code

About the author

Robert Williams is an internet application developer for the Salem Web Network.
E-mail me Send mail
Code Project Associate Logo
Go Daddy Deal of the Week: 30% off your order at GoDaddy.com! Offer expires 11/6/12

Recent comments

Archive

Authors

Tags

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.


Move View State to the Server

In his talk "Hidden Gems in ASP.NET 2.0" at Tech Ed 2007 Jeff Prosise showcased a number of really useful but little known features built into ASP.Net 2.0. The one feature that I implemented immediately on all my sites after his talk was his SessionPageStateAdapter class.

This class moves the bulk of the Viewstate text out of the hidden field on the page and into a session variable on the server, and requires remarkably few lines of code. Follow these steps to implement it on your site:

  1. Create  a folder in your application root and name it "App_Browsers"
  2. Add a new XML File to this folder. It doesn't matter what you call it as long as it has a ".browser" extention. I called mine "Default.browser".
  3. Paste in the following code:

    <browsers>
      <browser refID="Default">
        <controlAdapters>
          <adapter controlType="System.Web.UI.Page" adapterType="SessionPageStateAdapter" />
        </controlAdapters>
      </browser>
    </browsers>

  4. Create a new class file (in your App_Code dir for a web site or anywhere in a Web Project) and name it "SessionPageStateAdapter"
  5. Paste in the following code:

    using System;
    using System.Web.UI;
    /// <summary>
    /// Summary description for SessionPageStateAdapter
    /// </summary>
    public class SessionPageStateAdapter : System.Web.UI.Adapters.PageAdapter
    {
      public override PageStatePersister GetStatePersister()
      {
        return new SessionPageStatePersister(this.Page);
      }
    }

That's it! It reduced the viewstate field on this page (at the time of writing) from 9440 characters to just 129 characters!

  


Categories: ASP.Net
Posted by Williarob on Thursday, October 25, 2007 1:15 PM
Permalink | Comments (0) | Post RSSRSS comment feed