.NET Framework Class Library
PageStatePersister..::.StateFormatter Property

Gets an IStateFormatter object that is used to serialize and deserialize the state information contained in the ViewState and ControlState properties during calls to the Save and Load methods.

Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)
Syntax

Visual Basic (Declaration)
Protected ReadOnly Property StateFormatter As IStateFormatter
Visual Basic (Usage)
Dim value As IStateFormatter

value = Me.StateFormatter
C#
protected IStateFormatter StateFormatter { get; }
Visual C++
protected:
property IStateFormatter^ StateFormatter {
    IStateFormatter^ get ();
}
JScript
protected function get StateFormatter () : IStateFormatter

Property Value

Type: System.Web.UI..::.IStateFormatter
An instance of IStateFormatter that is used to serialize and deserialize object state.
Remarks

You can override the StateFormatter property to provide your own view state formatter.

Examples

The following code example demonstrates how a class that derives from the PageStatePersister class accesses the StateFormatter property to retrieve an ObjectStateFormatter object, which is the default implementation of the IStateFormatter interface, to serialize view state and control state to a stream. This code example is part of a larger example provided for the PageStatePersister class.

Visual Basic
'
' Persist any ViewState and ControlState.
'
Public Overrides Sub Save()

    If Not (ViewState Is Nothing) OrElse Not (ControlState Is Nothing) Then
        If Not (Page.Session Is Nothing) Then

            Dim stateStream As Stream
            stateStream = GetSecureStream()

            ' Write a state string, using the StateFormatter.
            Dim writer As New StreamWriter(stateStream)

            Dim formatter As IStateFormatter
            formatter = Me.StateFormatter

            Dim statePair As New Pair(ViewState, ControlState)

            Dim serializedState As String
            serializedState = formatter.Serialize(statePair)

            writer.Write(serializedState)
            writer.Close()
            stateStream.Close()
        Else
            Throw New InvalidOperationException("Session needed for StreamPageStatePersister.")
        End If
    End If
End Sub 'Save
C#
//
// Persist any ViewState and ControlState.
//
public override void Save()
{

    if (ViewState != null || ControlState != null)
    {
        if (Page.Session != null)
        {
            Stream stateStream = GetSecureStream();

            StreamWriter writer = new StreamWriter(stateStream);

            IStateFormatter formatter = this.StateFormatter;
            Pair statePair = new Pair(ViewState, ControlState);

            // Serialize the statePair object to a string.
            string serializedState = formatter.Serialize(statePair);

            writer.Write(serializedState);
            writer.Close();
            stateStream.Close();
        }
        else
            throw new InvalidOperationException("Session needed for StreamPageStatePersister.");
    }
}
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0
See Also

Reference

Tags :


Community Content

justingrant
property is, contrary to the docs, not overridable
> You can override the StateFormatter property to provide your own view state formatter

This is not true. This property is not marked virtual, so it can't be overridden. See http://aspadvice.com/blogs/mamanzes_blog/archive/2006/08/27/Save-some-space_2C00_-compress-that-ViewState.aspx for an example of hacking an override using reflection.
Tags : asp.net

Page view tracker