PageStatePersister.StateFormatter 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得 IStateFormatter 物件,對 ViewState 和 ControlState 方法呼叫時,這個物件會用來序列化和還原序列化 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 的執行個體,用來序列化和還原序列化物件狀態。
範例
下列程式碼範例示範衍生自 類別的 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 屬性,以提供自己的檢視狀態格式器。