Breyta

Deila með


StateBag.Item[String] Property

Definition

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

public:
 property System::Object ^ default[System::String ^] { System::Object ^ get(System::String ^ key); void set(System::String ^ key, System::Object ^ value); };
public object this[string key] { get; set; }
member this.Item(string) : obj with get, set
Default Public Property Item(key As String) As Object

Parameters

key
String

The key for the item.

Property Value

The specified item in the StateBag object.

Examples

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
{
    get 
    { 
        object o = ViewState["Text"]; 
        return (o == null)? String.Empty : (string)o;
    }

    set
    {
        ViewState["Text"] = value;
    }
}

' Add property values to view state with set; 
' retrieve them from view state with get.
Public Property [Text]() As String
    Get
        Dim o As Object = ViewState("Text")
        If (IsNothing(o)) Then
            Return String.Empty
        Else
            Return CStr(o)
        End If
    End Get
    Set(ByVal value As String)
        ViewState("Text") = value
    End Set
End Property

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 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.

Applies to

See also