Languages - DevSource
DevSource: Microsoft Developer Resource DevSource Home Sponsored by Microsoft Home Add Ons Architecture Languages Techniques Using VS Forums
Home arrow Languages arrow ASP.NET 2.0: Leaving ViewState on the Server Speeds up Page Delivery
ASP.NET 2.0: Leaving ViewState on the Server Speeds up Page Delivery
By Paul Kimmel

Rate This Article: Add This Article To:

Want to speed up the viewstate in your ASP.NET application? Paul Kimmel shows you how to use a relatively unknown little trick.

Introduction

I hope you made it to TechEd 2007. If you didn't or made it but didn't see Jeff Prosise's ASP.NET tips, I am including one of my favorite TechEd tips. First I'd like to mention that Jeff's presentation was one of the most polished, professional, and well-delivered presentations I have seen. If you get a chance to see Jeff from Wintellect, then do yourself the favor.

ADVERTISEMENT

The best thing about ASP.NET is that any client anywhere can run your application. The worst thing is that ASP.NET pages are disconnected so you have to pass around a bunch of plumbing to keep track of your page's state. One of the most common ways to keep page information is viewstate.

Viewstate is simply an encrypted hidden HTML field that is used to keep track of your page's state between round trips to the server. The problem is that with complex pages—like those with grids—viewstate can get very large very quickly. ASP.NET introduced the PageAdapter. The PageAdapter and a .browser file let you leave viewstate on the server. It's an easy technique once you know how and will add some pep to your busy web pages.

Implementing a PageAdapter

There are a lot of tips to improve ASP.NET application performance, such as use output caching; turn off session; disable viewstate it isn't used; and others.

Viewstate is on by default, and if you leave viewstate in its default setting then fairly static pages like http://www.softconcepts.com (see Figure 1) then your page will be storing a lot of viewstate (shown in Figure 2).

Figure 1: Static content in my homepage creates a lot of viewstate information.

Figure 2: A lot of viewstate from a very simple web page at http://www.softconcepts.com.

Controls like GridViews don't work very well without viewstate. So, if you need viewstate then you can add a single instance of a class that inherits from System.Web.UI.Adapaters.PageAdapter (see Listing 1) in your ASP.NET application and leave viewstate stored in session on the server. The benefit is that you get the benefit of viewstate without sending all of that scrambled text out to each client browser.

Listing 1: You can implement a PageAdapter by copying and pasting the code in this listing.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public class SessionPageStateAdapter :
    System.Web.UI.Adapters.PageAdapter
{
    public override PageStatePersister GetStatePersister ()
    {
        return new SessionPageStatePersister(this.Page);
    }
}

When you add a .cs class to your ASP.NET project you will be prompted to add this code to an App_Code folder, if you don't already have one. Answer yes to this prompt.

Adding a .Browser File to Register Your PageAdapter

The final step is to add a .browser file to your project by choosing Browser File in the Add New Item dialog box. You can name the .Browser file anything you'd like as long the extension is .browser. I used the default name of BrowserFile.browser and permitted the file to be stored in the App_Browsers folder. (Note: The App_Browsers folder will be created when you select the Browser File applet from the Add New Item dialog.)

Add the code in Listing 2 to your .browser file. (There will be some default browser definitions when you create the .browser file; I left them in the code below.)

Listing 2: Add the browser tag shown at the end of the listing. The adapterType attribute matches the name of your class.

<!--
    You can find existing browser definitions at
    <windir>\Microsoft.NET\Framework\<ver>\CONFIG\Browsers
-->
<browsers>
    <browser id="NewBrowser" parentID="Mozilla">
        <identification>
            <userAgent match="Unique User Agent Regular Expression" />
        </identification>

        <capture>
            <userAgent match="NewBrowser (?'version'\d+\.\d+)" />
        </capture>

        <capabilities>
            <capability name="browser" value="My New Browser" />
            <capability name="version" value="${version}" />
        </capabilities>
    </browser>

    <browser refID="Mozilla">
        <capabilities>
            <capability name="xml" value="true" />
        </capabilities>
    </browser>
    <browser refID="Default">
      <controlAdapters>
        <adapter controlType="System.Web.UI.Page"
          adapterType="SessionPageStateAdapter" />
      </controlAdapters>
    </browser>

</browsers>

Compile and run your application. The PageAdapter and the .browser file works across your application, and these two elements will eliminate most of your app's viewstate for every page.

There is one caution: if a user opens multiple instances of your web application with the same session id then this technique may get wonky. You should get a separate session id for each instance of Ineternet Explorer. But IE7 may share session ids if you open multiple tabs.

Summary

If you inherit from PageAdapter and implement a .browser file and you can leave all of that heavy viewstate text in session on the server. The result is that complex pages that need viewstate render a little snappier. For simple pages or mostly static pages you can disable viewstate and use page caching.

For more ASP.NET tips from Jeff Prosise checkout http://msdn.microsoft.com/msdnmag/issues/06/07/WebAppFollies/default.aspx#S4. For more great tips on .NET check out my upcoming articles here at devsource.com.




Discuss ASP.NET 2.0: Leaving ViewState on the Server Speeds up Page Delivery
 
>>> Be the FIRST to comment on this article!
 

 
 
>>> More Languages Articles          >>> More By Paul Kimmel
 



Microsoft's Future: A Chat With Their CTO, Barry Briggs

Play Video >

All Videos >

Julia explores the Robotics Studio!

Read now >

Messages to Bill Gates!

Read now >

View Now
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.