PageStatePersister.StateFormatter Properti
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
IStateFormatter Mendapatkan objek yang digunakan untuk menserialisasikan dan mendeserialisasi informasi status yang terkandung dalam ViewState properti dan ControlState selama panggilan ke Save() metode dan 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
Nilai Properti
Instans IStateFormatter yang digunakan untuk menserialisasikan dan mendeserialisasi status objek.
Contoh
Contoh kode berikut menunjukkan bagaimana kelas yang berasal dari PageStatePersister kelas mengakses StateFormatter properti untuk mengambil ObjectStateFormatter objek, yang merupakan implementasi IStateFormatter default antarmuka, untuk membuat serialisasi status tampilan dan status kontrol ke aliran. Contoh kode ini adalah bagian dari contoh yang lebih besar yang disediakan untuk PageStatePersister kelas .
//
// 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
Keterangan
Anda dapat mengambil alih StateFormatter properti untuk menyediakan pemformat status tampilan Anda sendiri.