Export (0) Print
Expand All

StateBag.Item Property

Gets or sets the value of an item stored in the StateBag object.

Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)

public Object this[
	string key
] { get; set; }


Type: System.String

The key for the item.

Property Value

Type: System.Object
The specified item in the StateBag object.

Using this member is the simplest way to save and retrieve view-state values for a control or a page.

If an item is not already stored in the StateBag object when you set this property, its key/value pair is added to the collection. If you set this property to null before the TrackViewState method is called on an item, it is removed from the StateBag object. Otherwise, when you set this property to null the key is saved to allow tracking of the item's view state.

The following code example demonstrates a property that saves its name and value as a key/value pair to the Control.ViewState property. The ViewState property is an instance of the StateBag class.

        // Add property values to view state with set; 
        // retrieve them from view state with get. 
        public String Text
                object o = ViewState["Text"]; 
                return (o == null)? String.Empty : (string)o;

                ViewState["Text"] = value;

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

© 2014 Microsoft