PageStatePersister.Save 方法

定义

在从内存中卸载 Page 对象时,由派生类重写以对所保留的状态信息进行序列化。

public:
 abstract void Save();
public abstract void Save ();
abstract member Save : unit -> unit
Public MustOverride Sub Save ()

示例

下面的代码示例演示如何从 PageStatePersister 类派生的类实现 Save 将视图状态保存到持久性介质的方法。 该StreamPageStatePersister对象用于IStateFormatter序列化属性和ControlState属性的内容ViewState。 此代码示例是为类提供的大型示例的 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

注解

派生自类的 PageStatePersister 类实现 Save 该方法,以便将内容 ViewStateControlState 属性保存到持久性介质。

适用于