Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

PageStatePersister.ViewState Property

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.

Namespace: System.Web.UI
Assembly: System.Web (in system.web.dll)
public Object ViewState { get; set; }
/** @property */
public Object get_ViewState ()

/** @property */
public void set_ViewState (Object value)

public function get ViewState () : Object

public function set ViewState (value : Object)

Not applicable.

Property Value

An object that contains view state data.

View state is a dictionary of state data that Web server controls need to function and render themselves. Control developers typically access the view state object using the ViewState property. 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. For more information on using ViewState and control state when developing controls, see Developing Custom ASP.NET Server Controls.

The following code example demonstrates how a class that derives from the PageStatePersister class initializes the ViewState property. In 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. When 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. This 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();
}

Windows 98, Windows Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.