September 15, 2019
Hot Topics:

De-Sludging ASP.NET Pages with PageAdapter

  • July 9, 2007
  • By Paul Kimmel
  • Send Email »
  • More Articles »


You know what VIEWSTATE is? It's that encrypted gobbley-gook in a hidden field that makes stateless web pages (in ASP.NET) keep track of a page for each user between round trips to the server. VIEWSTATE is what keeps track of the text you put in a TextBox. VIEWSTATE is what keeps track of events that need to fire. And, VIEWSTATE is a lot of scrambled eggs that goes back and forth between each client and your server (or servers).

By now, you have probably heard that one should disable VIEWSTATE in ASP.NET applications for static content or when you know you don't need it. This is a tip for improving performance. For static web sites, this is a good tip. For dynamic websites—applications—VIEWSTATE is pretty hard to do without. However, this doesn't mean you have to card it around with each page.

In this article, I will show you how to use VIEWSTATE but leave it on the server. The technique was introduced in ASP.NET 2.0. It's a great technique, will significantly reduce the size of the rendered HTML returned to clients, still permit VIEWSTATE dependent behaviors to work, and make your web pages a little snappier.

Implementing a Custom PageAdapter

The web scales because each client gets its data and disconnects. Things such as Session ID, cookies, and VIEWSTATE help web servers figure out what to do when a page is posted back. Content-only (or content-mostly) sites like http://www.softconcepts.com don't really need a lot of state information, so it can be turned off to improve performance a bit. Further, sites like mine can use page caching to get a little better performance.

By implementing a custom PageAdapter, you can use VIEWSTATE but ASP.NET will use the Session ID and automatically keep and track VIEWSTATE on the server for each user. Complex web applications with a lot of viewstate will render a little faster and run a little snappier. To implement, simply add a class with the code in Listing 1 to your web application (and you will need a .Browser file, covered in the next section.)

Listing 1: To use VIEWSTATE but leave it on the server, add a class file with the following code.

Imports Microsoft.VisualBasic

Public Class MyPageAdapter
   Inherits System.Web.UI.Adapters.PageAdapter

   Public Overrides Function GetStatePersister() _
      As System.Web.UI.PageStatePersister
      Return New  SessionPageStatePersister(Me.Page)
   End Function

End Class

Visual Studio will add the code in Listing 1 to the App_Code folder.

Adding a .Browser File to Your Website

Next, you need to tell your web application that you have added a PageAdapter. Select your web project, choose Add New Item, and select the Browser File applet (see Figure 1). Listing 2 shows the default content and the <controlAdapters> tag for MyPageAdapter.

Click here for a larger image.

Figure 1: An easy item to miss, adding a Browser File lets you tell your web application about PageAdapters.

Listing 2: The .browser file—it can be named anything—containing default information added by the template and the tags added used to load myPageAdapter.

   You can find existing browser definitions at
   <browser id="NewBrowser" parentID="Mozilla">
         <userAgent match="Unique User Agent Regular Expression" />

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

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

   <browser refID="Mozilla">
         <capability name="xml" value="true" />

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

Page 1 of 2

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date