PageStatePersister.StateFormatter 属性

定义

获取一个 IStateFormatter 对象,该对象在对 ViewStateControlState 方法的调用期间用于对包含在 Save()Load() 属性中的状态信息进行序列化和反序列化。

protected:
 property System::Web::UI::IStateFormatter ^ StateFormatter { System::Web::UI::IStateFormatter ^ get(); };
protected System.Web.UI.IStateFormatter StateFormatter { get; }
member this.StateFormatter : System.Web.UI.IStateFormatter
Protected ReadOnly Property StateFormatter As IStateFormatter

属性值

IStateFormatter

IStateFormatter 的一个实例,用于对对象状态进行序列化和反序列化。

示例

下面的代码示例演示派生自 PageStatePersister 类的类如何访问 StateFormatter 属性以检索 ObjectStateFormatter 接口的默认实现 IStateFormatter 的对象,以便将视图状态和控制状态序列化为流。 此代码示例是为类提供的大型示例的 PageStatePersister 一部分。

//
// 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.");
        }
    }
}
'
' 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

注解

可以重写该 StateFormatter 属性以提供自己的视图状态格式化程序。

适用于