Welcome to AspAdvice Sign in | Join | Help

Server Side Viewstate in ASP.NET 2.0

We had viewstate problems when we rolled out schwans.com using ASP.NET 1.0 (some user agents could not handle the large hidden field and other times we had corruptions that caused the MAC check to fail on postback, read about it at http://aspalliance.com/72).  We solved the problem by writing our own solution for storing the viewstate in Session.  ASP.NET 2.0 now provides built in support for this!  It utilizes a page adapter and the new SessionPageStatePersister class. 

The first step is to create a page adapter that simply returns an instance of the SessionPageStatePersister class rather than the default HiddenFieldPageStatePersister class which stores viewstate in a hidden field on the client.  Create a class library that contains the following and compile into an assembly:

using System;
using System.Collections.Generic;
using System.Text;

using System.Web.UI;

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

 

Then create a .browser that specifies that the new page adapter should be used for all browsers (example is named pageStateAdapter.browser and placed in App_Browsers folder locally with the web site):
<browsers>
    <browser refID="Default">
        <controlAdapters>
           <adapter controlType="System.Web.UI.Page" adapterType="RJB.PageStateAdapter" />
        </controlAdapters>
    </browser>
</browsers>

 

Now as requests come in from any type of browser it will use the new adapter which returns a SessionPageStatePersister instance.  You can verify that it is working properly by viewing a page on the site then use trace.axd and view the session information and verify that it contains viewstate info (__VIEWSTATEQUEUE and __SESSIONSTATE... entries).  Viewing the client page source will also show that the majority of the viewstate is no longer stored on the client (not sure what is left on the client...)

 

 

Published Wednesday, November 16, 2005 1:03 PM by robertb

Comments

# re: Server Side Viewstate in ASP.NET 2.0

Hey, i get the following error when I try to run my application :

Parser Error Message: Could not load type 'RJB.PageStateAdapter'.

it occurs at this line : Line 4:        <adapter controlType="System.Web.UI.Page" adapterType="RJB.PageStateAdapter" />

if anyone can help me, please reply!

Tuesday, September 25, 2007 10:41 AM by Imperialist

# re: Server Side Viewstate in ASP.NET 2.0

Did you create a class that contains the class listed above?  You can either place the class in the App_Code folder, or place it in another assembly (which can be placed in the bin or GAC).  Please email me at robertb@aspalliance.com if you have any other questions.

Friday, September 28, 2007 7:50 AM by robertb

# re: Server Side Viewstate in ASP.NET 2.0

I used your code, and ran my app. It worked(no errors), but I do not have my querystring on the postback. Any ideas?

Tuesday, October 09, 2007 11:01 AM by rkmoray

# re: Server Side Viewstate in ASP.NET 2.0

If you comment out the <adapter> line in the .browser file and run your app again does it work now?  If it works without that line, but fails with that line, please email me at robertb@aspalliance.com so we can discuss your situation in more detail.

Tuesday, October 09, 2007 2:06 PM by robertb

# server side viewstate &laquo; Logs [dotnet and non-dotnet]

Wednesday, October 31, 2007 1:14 PM by server side viewstate « Logs [dotnet and non-dotnet]

# Viewstate compression &laquo; Logs [dotnet and non-dotnet]

Wednesday, October 31, 2007 1:14 PM by Viewstate compression « Logs [dotnet and non-dotnet]

# Efficient Server-Side View State Persistence

Efficient Server-Side View State Persistence

Tuesday, August 05, 2008 5:38 PM by Use little words...

# re: Server Side Viewstate in ASP.NET 2.0

Hi,

I have used your method to implement server side viewstate in asp.net3.5,but i am facing a problem:- I have two user controls on the page and rest are drop-downs and a query button. One user control is date control with autopostpack=false; other is with autopostback=True" now what is happening is that when ever i am selecting date from more than 2months,and invoking post back from the other usercontrol, all my drop downs are getting cleared.

I have done the following oberservations:-

if I comment out the <adapter> line in the .browser file every thing works fine and also

If I make the datecontrol autppostback=True.

but as its not good to initiate a postback without any reason, I want you to suggest me some remedy to this.

I hope for a fast reply plz help

Monday, February 23, 2009 5:57 AM by urvi

# re: Server Side Viewstate in ASP.NET 2.0

How would I implement this if there is a form on a master page... I keep getting corrupted viewstate.

Tuesday, June 02, 2009 8:15 AM by sircutbreaker

# Sircutbreaker

Email me at robertb at aspalliance.com and I will help you troubleshoot.  We use this on projects that contain master pages and it works fine.

Tuesday, June 02, 2009 8:32 AM by robertb

# ¿Improve UPDATEPANELS performance with server side Viewstate?

Hi, I have a web application based on ASP.NET 3.5, using intensively ASP.NET AJAX Framework (Basically

Friday, June 26, 2009 6:04 PM by ASP.NET AJAX Forum Posts
Anonymous comments are disabled