This documentation is archived and is not being maintained.

IStateFormatter Interface

Defines methods that a type implements to serialize and deserialize an object graph.

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

Public Interface IStateFormatter

The IStateFormatter type exposes the following members.

Public methodDeserializeDeserializes an object state graph from its serialized string form.
Public methodSerializeSerializes ASP.NET Web server control state to string form.

The IStateFormatter interface defines methods that a type can implement to serialize and deserialize the state that an ASP.NET Web server control maintains in its ViewState property. This infrastructure is used by classes that derive from the PageStatePersister class to maintain an ASP.NET page's state between requests. By default, ASP.NET page state is serialized and deserialized by an instance of the ObjectStateFormatter class; however, site and adapter developers can implement the IStateFormatter interface on their own types to perform this work.

For more information about Web server control state management and view state, see ASP.NET State Management Overview and Dynamic Web Server Controls and View State.

The following code example demonstrates how to create a PageStatePersister object that saves view and control state to a stream on the Web server. The StreamPageStatePersister class demonstrates how to override the Load and Save methods to extract and save page state information. These methods use the IStateFormatter interface inherited from the PageStatePersister class to serialize and deserialize view state. This code example is part of a larger example provided for the PageStatePersister class.

Imports System
Imports System.IO
Imports System.Security.Permissions
Imports System.Web
Imports System.Web.UI

Namespace Samples.AspNet.VB

    ' The StreamPageStatePersister is an example view state
    ' persistence mechanism that persists view and control
    ' state on the Web server.
    <AspNetHostingPermission(SecurityAction.Demand, Level:=AspNetHostingPermissionLevel.Minimal)> _
    Public Class StreamPageStatePersister
        Inherits PageStatePersister

        Public Sub New(ByVal page As Page)
        End Sub 'New

        ' Load ViewState and ControlState.
        Public Overrides Sub Load()

            Dim stateStream As Stream
            stateStream = GetSecureStream()

            ' Read the state string, using the StateFormatter.
            Dim reader As New StreamReader(stateStream)

            Dim serializedStatePair As String
            serializedStatePair = reader.ReadToEnd
            Dim statePair As Pair

            Dim formatter As IStateFormatter
            formatter = Me.StateFormatter

            ' Deserilize returns the Pair object that is serialized in
            ' the Save method.      
            statePair = CType(formatter.Deserialize(serializedStatePair), Pair)

            ViewState = statePair.First
            ControlState = statePair.Second
        End Sub ' Load

        ' 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)

                    Throw New InvalidOperationException("Session needed for StreamPageStatePersister.")
                End If
            End If
        End Sub 'Save
        ' Return a secure Stream for your environment.
        Private Function GetSecureStream() As Stream
            ' You must provide the implementation to build
            ' a secure Stream for your environment.
            Return Nothing
        End Function
    End Class
End Namespace

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.