Potty Little Details

Just another WordPress.com weblog

Alternative Storage for ViewState

with one comment

Storing ViewState within the returned HTML page is convenient, but when ViewState gets to be more than 20K or 30K, it may be time to consider alternative locations for storage. You might want to store ViewState in the ASP.NET Session object, on the file system, or in the database to minimize the amount of data shipped back and forth to the user’s browser.

The most advanced ViewState hacking product that I’ve seen is the Flesk ViewStateOptimizer (http://www.flesk.net), as it enables you to compress ViewState and move it to a file or to the session. When moving ViewState to the Session object, you have to take into consideration how ViewState is supposed to work. Remember that when ViewState is stored in a page’s hidden form field, the ViewState for a page is literally stored along with the page itself. This is an important point, so read it again. When you choose to store ViewState elsewhere—separated from the page—you need a way to correlate the two. Your first reaction might be to think that each user needs a copy of the ViewState for each page they visit. However, it’s not as simple as the equation “users * page = number of ViewState instances,” as a user can and will visit a page multiple times. Each page instance needs its own copy of ViewState.

There are many ways to squirrel away ViewState. Some folks think that creating unique files on the file system and then collecting them later is a good technique. Personally, I would always rather add more memory than be bound to the always slower disk. No matter where you store ViewState, if you store it outside the page, then you’ll need some process to later delete the older bits of state. This could take the form of a scheduled task that deletes files or a SQL Server job that removes rows from a database.

Folks who really want to get their ViewState out of the page have tried many ways to solve this problem. Snippet below shows a hack that stores the ViewState value within the user’s ASP.NET Session using a unique key per request. The ViewState string value that is usually sent out in a hidden form field is stored in the Session using the unique key, and then that considerably smaller key is put into a hidden form field instead. Therefore, each page gets its own Guid, as each HTTP request is unique. This Guid is declared as the ViewState “key” and is stored as its own hidden form field. This key is then used to store the ViewState in the Session.
Storing ViewState in the ASP.NET Session

private string _pageGuid = null;
public string PageGuid
{
get
{
//Do we have it already? Check the Form, this could be a post back
if (_pageGuid == null)
_pageGuid = this.Request.Form[“__VIEWSTATE_KEY”];
//No? We’ll need one soon.
if (_pageGuid == null)
_pageGuid = Guid.NewGuid().ToString();
return _pageGuid;
}
set
{
_pageGuid = value;
}
}

protected override object LoadPageStateFromPersistenceMedium()
{
return Session[this.PageGuid];
}

protected override void SavePageStateToPersistenceMedium(object viewState)
{
RegisterHiddenField(“__VIEWSTATE_KEY”, this.PageGuid);
Session[this.PageGuid] = viewState;
}

The load and save methods are very simple, just storing the PageGuid in the Session object and the ViewState object within the Session. The real magic happens in the new PageGuid page-level property. When the PageGuid is requested the first time, the form is checked for the unique key. If it’s not there, a new key is created because it will likely be needed soon after.

Advertisements

Written by oneil

September 9, 2008 at 4:30 pm

Posted in ASP DOT NET

One Response

Subscribe to comments with RSS.

  1. At this approach a session must be kept in Sessionstate service.
    But MS Sessionstate service does not suit for storage of large objects, that is why after some time it simply will be recurved..

    zyb

    September 12, 2008 at 10:01 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: