About

ViewStateToDB is a simple assembly (.DLL) that can be plugged into any existing ASP.NET Web Form web site/application in order to move the persistence of the view state from the page (HTML) to a database. By storing the view state in the database the size of the page is greatly reduced (=saves you bandwidth) and the security is increased since the view state can no longer be viewed by everyone.

Features

  • Stores the view state and control state in a database.
  • Saves bandwidth since the size of the page get's smaller.
  • Increased security since the view state no longer can be viewed by everyone.
  • Can be plugged into any existing ASP.NET Web Form project.
  • Supports MS SQL-Server, other databases can be added by request.
  • SQL-cleanup script for removing old view state.

How it works

  1. On the first page request, a hidden field with a unique id is added to the page and the view state is added to the database.
  2. After a post back, the unique id from the hidden field is retrieved, the database is queried and the view state is restored.
  3. Old view states are removed from the database by running the SQL cleanup script.

Getting started

  1. Get the latest release.
  2. Add a reference to ViewStateDB.dll from your project.
  3. Create a new class with the code from the sample code below, or if you already have a base page, modify it to contain the code.
  4. Make sure all your ASPX pages inherit from the BasePage class instead of System.Web.UI.Page.
  5. Run the SQL code from the file InstallSql.txt in the database where the view state data will be stored.
  6. Finished. The view state shall now be stored in the database. Verify it by making sure that the hidden field "VIEWSTATEID" is rendered to the page.

Sample code

using System.Configuration;
using System.Web.UI;
using ViewStateToDb;

public class BasePage : Page
{
	private PageStatePersister _pageStatePersister;

	protected override PageStatePersister PageStatePersister
	{
		get
		{
			if (_pageStatePersister == null)
			{
				var configuration = new ViewStateToDbConfiguration();
				configuration .ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

				_pageStatePersister = new DbPageStatePersister(this, configuration);
			}

			return _pageStatePersister;
		}
	}

	public BasePage()
	{
	}
}

If needed, the name of the hidden field and the table name in the database can be customized by setting the right properties on the configuration object.

Last edited Nov 16, 2010 at 6:04 AM by kindblad, version 10