This documentation is archived and is not being maintained.

StateBag.Item Property

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

[C#] In C#, this property is the indexer for the StateBag class.

[Visual Basic]
Public Default Property Item( _
   ByVal key As String _
) As Object
[C#]
public object this[
 string key
] {get; set;}
[C++]
public: __property Object* get_Item(
 String* key
);
public: __property void set_Item(
 String* key,
   Object*
);
[JScript]
returnValue = StateBagObject.Item(key);
StateBagObject.Item(key) = returnValue;
-or-
returnValue = StateBagObject(key);
StateBagObject(key) = returnValue;

[JScript] In JScript, you can use the default indexed properties defined by a type, but you cannot explicitly define your own. However, specifying the expando attribute on a class automatically provides a default indexed property whose type is Object and whose index type is String.

Arguments [JScript]

key
The key for the item.

Parameters [Visual Basic, C#, C++]

key
The key for the item.

Property Value

The specified item in the StateBag object.

Remarks

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 a null reference (Nothing in Visual Basic) before the TrackViewState method is called on an item, it is removed from the StateBag object. Otherwise, when you set this property to a null reference (Nothing) the key is saved to allow tracking of the item's view state.

Example

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

[Visual Basic] 
' Add property values to view state with set; 
' retrieve them from view state with get.
Public Property [Text] As String
   Get
       Return CStr(ViewState("Text"))
   End Get
   Set
       ViewState("Text") = Value
   End Set
End Property


[C#] 
// Add property values to view state with set;
// retrieve them from view state with get.
public String Text {
   get {
       return (String) ViewState["Text"];
   }
   set {
       ViewState["Text"] = value;
   }
}


[C++] 
// Add property values to view state with set;
// retrieve them from view state with get.
public:
__property String * get_Text()
{
   return dynamic_cast<String*>(ViewState->Item[S"Text"]);
}

__property void set_Text(String * value)
{
   ViewState->Item[S"Text"] = value;
}

[JScript] 
// Add property values to view state with set;
// retrieve them from view state with get.
public function get Text() : String 
{
       return String(ViewState["Text"]);
}

public function set Text(value : String)
{
       ViewState["Text"] = value;
}

Requirements

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

See Also

StateBag Class | StateBag Members | System.Web.UI Namespace | Add | Remove | ViewState | Introduction to Web Forms State Management

Show: