PageStatePersister.ViewState 属性

定义

获取或设置一个对象,该对象表示当前 Page 对象包含的控件用于跨 Web 服务器的 HTTP 请求保留的数据。Gets or sets an object that represents the data that controls contained by the current Page object use to persist across HTTP requests to the Web server.

public:
 property System::Object ^ ViewState { System::Object ^ get(); void set(System::Object ^ value); };
public object ViewState { get; set; }
member this.ViewState : obj with get, set
Public Property ViewState As Object

属性值

Object

包含视图状态数据的对象。An object that contains view state data.

示例

下面的代码示例演示如何将派生自类的类 PageStatePersister 初始化 ViewState 属性。The following code example demonstrates how a class that derives from the PageStatePersister class initializes the ViewState property. 在此示例中,已将 ViewState 属性分配给 First 对象的字段 Pair ,并使用 ObjectStateFormatter 对象(该接口的实例)对其进行序列化 IStateFormatterIn this example, the ViewState property has been assigned to the First field of a Pair object, and serialized using the ObjectStateFormatter object, which is an instance of the IStateFormatter interface. Load调用方法时, ObjectStateFormatter 接口用于对视图状态信息进行反序列化,并 ViewState 从生成的 Pair 对象的字段初始化属性 FirstWhen the Load method is called, the ObjectStateFormatter interface is used to deserialize view state information, and the ViewState property is initialized from the resulting Pair object's First field. 此代码示例是为类提供的更大示例的一部分 PageStatePersisterThis code example is part of a larger example provided for the PageStatePersister class.

//
// Load ViewState and ControlState.
//
public override void Load()
{
    Stream stateStream = GetSecureStream();

    // Read the state string, using the StateFormatter.
    StreamReader reader = new StreamReader(stateStream);

    IStateFormatter formatter = this.StateFormatter;
    string fileContents = reader.ReadToEnd();

    // Deserilize returns the Pair object that is serialized in
    // the Save method.
    Pair statePair = (Pair)formatter.Deserialize(fileContents);

    ViewState = statePair.First;
    ControlState = statePair.Second;
    reader.Close();
    stateStream.Close();
}
'
' Load ViewState and ControlState.
'
Public Overrides Sub Load()

    Dim stateStream As Stream
    stateStream = GetSecureStream()

    ' Read the state string, using the StateFormatter.
    Dim reader As New StreamReader(stateStream)

    Dim serializedStatePair As String
    serializedStatePair = reader.ReadToEnd
    Dim statePair As Pair

    Dim formatter As IStateFormatter
    formatter = Me.StateFormatter

    ' Deserilize returns the Pair object that is serialized in
    ' the Save method.      
    statePair = CType(formatter.Deserialize(serializedStatePair), Pair)

    ViewState = statePair.First
    ControlState = statePair.Second
    reader.Close()
    stateStream.Close()
End Sub

注解

视图状态是 Web 服务器控件需要运行并呈现自身的状态数据字典。View state is a dictionary of state data that Web server controls need to function and render themselves. 控件开发人员通常使用属性访问视图状态对象 ViewStateControl developers typically access the view state object using the ViewState property. 当在级别禁用视图状态时,视图状态将受到影响 Page ,因此在这些情况下,控件可能不会正常运行。View state is affected when view state is disabled at the Page level, and as a result controls might not behave correctly in these scenarios. 有关 ViewState 开发控件时使用和控件状态的详细信息,请参阅 开发自定义 ASP.NET 服务器控件For more information on using ViewState and control state when developing controls, see Developing Custom ASP.NET Server Controls.

适用于