共用方式為


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 將 和 ControlState 屬性的內容 ViewState 保存到持續性媒體。

適用於