StateBag.Item[String] 屬性

定義

取得或設定儲存於 StateBag 物件中的項目值。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

參數

key
String

項目的索引鍵。The key for the item.

屬性值

StateBag 物件中的指定項目。The specified item in the StateBag object.

範例

下列程式碼範例示範的屬性會將其名稱和值儲存為 Control.ViewState 屬性的索引鍵/值組。The following code example demonstrates a property that saves its name and value as a key/value pair to the Control.ViewState property. ViewState 屬性是 StateBag 類別的實例。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

備註

使用這個成員是儲存和抓取控制項或頁面之檢視狀態值的最簡單方式。Using this member is the simplest way to save and retrieve view-state values for a control or a page.

當您設定此屬性時,如果專案尚未儲存在 StateBag 物件中,它的索引鍵/值組會加入至集合。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. 如果您在專案上呼叫 TrackViewState 方法之前,將此屬性設定為 null,則會從 StateBag 物件中將它移除。If you set this property to null before the TrackViewState method is called on an item, it is removed from the StateBag object. 否則,當您將此屬性設定為時 null 會儲存金鑰,以允許追蹤專案的檢視狀態。Otherwise, when you set this property to null the key is saved to allow tracking of the item's view state.

適用於

另請參閱