Cache.Item[String] 属性

定义

获取或设置指定键处的缓存项。

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

表示缓存项的键的 String 对象。

属性值

指定的缓存项。

示例

以下示例使用 Item 属性检索与键关联的 Key1 缓存对象的值。 然后,HttpResponse.Write它使用 方法将值和介绍性文本以及 B HTML 元素写入Web Forms页。

Response.Write("Value of cache key: <B>" + Server.HtmlEncode(Cache["Key1"] as string) + "</B>");
Response.Write("Value of cache key: <B>" + Server.HtmlEncode(CType(Cache("Key1"),String)) + "</B>")

以下示例演示如何使用此属性将文本框的值插入缓存中。

private void cmdAdd_Click(Object objSender, EventArgs objArgs)
{
    if (txtName.Text != "")
    {
        // Add this item to the cache.
        Cache[txtName.Text] = txtValue.Text;
    }
}

Private Sub cmdAdd_Click(objSender As Object, objArgs As EventArgs)
  If txtName.Text <> "" Then
    ' Add this item to the cache.
  Cache(txtName.Text) = txtValue.Text
  End If
End Sub

注解

可以使用此属性检索指定缓存项的值,或将项和键添加到缓存中。 使用 Item[] 属性添加缓存项等效于调用 Cache.Insert 方法。

适用于

另请参阅