UserControl.Cache 屬性


取得 Cache 物件,這個物件與包含使用者控制項的應用程式關聯。Gets the Cache object that is associated with the application that contains the user control.

 property System::Web::Caching::Cache ^ Cache { System::Web::Caching::Cache ^ get(); };
public System.Web.Caching.Cache Cache { get; }
member this.Cache : System.Web.Caching.Cache
Public ReadOnly Property Cache As Cache



存放使用者控制項資料的 Cache 物件。The Cache object in which to store the user control's data.



下列範例會使用屬性,將 Cache Text Web 服務器控制項的屬性值儲存 Label txtValueCache 與使用者控制項應用程式相關聯的物件中。The following example uses the Cache property to store the Text property value of a Label Web server control, txtValue, in the Cache object associated with the user control's application. 它會使用 Cache 屬性來進行這項作業,並將索引鍵參數值指派給專案 txtName.TextIt uses the Cache property to do so, assigning the item a key parameter value of txtName.Text.


這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。This example has a text box that accepts user input, which is a potential security threat. 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。By default, ASP.NET Web pages validate that user input does not include script or HTML elements. 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。For more information, see Script Exploits Overview.

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


Cache 讓您儲存資料以供稍後抓取,並在應用程式間共用。The Cache allows you to store data for later retrieval, and is shared across the application. 您儲存的資料與目前的頁面或使用者會話無關。The data that you store is independent of the current page or user session. 透過這個屬性存取資料,以提高頁面或應用程式效能(如果建立資料的速度很慢)。Access data through this property to boost page or application performance if creating the data is slow. 如需使用快取的詳細資訊,請參閱快取 應用程式資料For more information about using the Cache, see Caching Application Data.