Click here to Skip to main content
15,887,822 members
Articles / Web Development / ASP.NET

Manage ViewState Using ASP.NET 2.0 Provider Architecture

Rate me:
Please Sign up or sign in to vote.
3.93/5 (5 votes)
5 Jun 2007CPOL7 min read 78.2K   752   49  
Serverside ViewState management using custom providers based on ASP.NET 2.0 provider pattern architecture.
Imports System.IO
Imports System.Web.UI
Imports System.Collections.Specialized
Imports System.Text


Namespace System.Web.Configuration.Providers

	''' <summary>
	'''	ViewState Provider that compresses the view state string and stores it in page's hidden variable. 
	''' </summary>
	''' <remarks>
	''' Scott Galloway has a blog entry where he discusses his experiences with using #ziplib library to compress the view state. 
	''' For more info, please visit http://www.mostlylucid.co.uk/ 
	''' Inspiration from implementation in http://www.codeproject.com/aspnet/ViewStateProvider.asp 
	''' Based on code in http://msdn2.microsoft.com/en-us/library/aa479028.aspx
	''' </remarks>
	''' <history>
	''' 	[Oleg Sobol]	9/26/2006	Created
	''' </history>
	''' -----------------------------------------------------------------------------
	Public Class CompressionViewStateProvider
		Inherits ViewStateProvider


		Private _applicationName As String
		Private _los As New LosFormatter
		Private _lockLoad As New Object
		Private _lockSave As New Object
		Private _viewStateKeyName As String = "__COMPRESSEDVIEWSTATE"

#Region " Properties "

		Public Overrides Property ApplicationName() As String
			Get
				Return _applicationName
			End Get
			Set(ByVal value As String)
				_applicationName = value
			End Set
		End Property

		Public Overrides Property ViewStateKeyName() As String
			Get
				Return _viewStateKeyName
			End Get
			Set(ByVal value As String)
				_viewStateKeyName = value
			End Set
		End Property
#End Region


		Public Overloads Overrides Sub Initialize(ByVal name As String, ByVal config As NameValueCollection)
			' Verify that config isn't null
			If config Is Nothing Then Throw New ArgumentNullException("config")
			' Assign the provider a default name if it doesn't have one
			If String.IsNullOrEmpty(name) Then name = "CompressionViewStateProvider"
			' Add a default "description" attribute to config if the attribute doesn�t exist or is empty
			If String.IsNullOrEmpty(config("description")) Then
				config.Remove("description")
				config.Add("description", "Compression viewstate provider")
			End If
			' Call the base class's Initialize method
			MyBase.Initialize(name, config)

			' Initialize _applicationName
			_applicationName = config("applicationName")
			If String.IsNullOrEmpty(_applicationName) Then _applicationName = "/"
			config.Remove("applicationName")


			' Throw an exception if unrecognized attributes remain
			If config.Count > 0 Then
				Dim attr As String = config.GetKey(0)
				If Not String.IsNullOrEmpty(attr) Then Throw New ViewStateProviderException("Unrecognized attribute: " + attr)
			End If
		End Sub


#Region " ViewStateProvider specific behaviors "
		Public Overloads Overrides Function LoadPageState(ByVal pControl As Control) As Object
			SyncLock _lockLoad
				Dim vsString As String = CType(pControl, Page).Request.Form(_viewStateKeyName)
				Dim outStr As String = Util.Compression.DeCompress(vsString)
				Return _los.Deserialize(outStr)
			End SyncLock
		End Function

		Public Overloads Overrides Sub SavePageState(ByVal pControl As Control, ByVal viewState As Object)
			SyncLock _lockSave
				Dim writer As New StringWriter
				_los.Serialize(writer, viewState)
				Dim outStr As String = Util.Compression.Compress(writer.ToString)
				CType(pControl, Page).ClientScript.RegisterHiddenField(_viewStateKeyName, outStr)
				writer.Dispose()
			End SyncLock
		End Sub
#End Region

	End Class



End Namespace


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
Oleg Sobol has been developing web applications in ASP.NET using VB.NET for the past three years. Currently working for a leading online higher education provider, developing new online initiatives and supporting multiple education platforms.

Comments and Discussions