PartialCachingAttribute 類別

定義

定義 Web Forms 使用者控制 (.ascx 檔案) 用來指出其輸出快取方式的元數據屬性。 此類別無法獲得繼承。

public ref class PartialCachingAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Class)]
public sealed class PartialCachingAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Class)>]
type PartialCachingAttribute = class
    inherit Attribute
Public NotInheritable Class PartialCachingAttribute
Inherits Attribute
繼承
PartialCachingAttribute
屬性

範例

以下程式碼範例展示了使用 PartialCachingAttribute。 此範例分為三部分:

  • 一個偏類別, ctlMine繼承自 UserControl 基底類別,且該屬性被套用於其 PartialCachingAttribute 上。

  • 一個用於 ctlMine 部分類別的使用者控制項。

  • 一個網頁表單頁面,負責管理使用者控制。

範例的第一部分展示了一個部分類別,該類別繼承自基 UserControl 底類別,並將該 PartialCachingAttribute 屬性套用於其上。 在此範例中,屬性指定使用者控制項應快取 20 秒。

// [filename partialcache.cs]
// Create a code-behind user control that is cached
// for 20 seconds using the PartialCachingAttribute class.
// This control uses a DataGrid server control to display
// XML data.
using System;
using System.IO;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Samples.AspNet.CS.Controls
{

    // Set the PartialCachingAttribute.Duration property to 20 seconds.
    [PartialCaching(20)]
    public partial class ctlMine : UserControl
    {

        protected void Page_Load(Object Src, EventArgs E)
        {
            DataSet ds = new DataSet();

            FileStream fs = new FileStream(Server.MapPath("schemadata.xml"), FileMode.Open, FileAccess.Read);
            StreamReader reader = new StreamReader(fs);
            ds.ReadXml(reader);
            fs.Close();

            DataView Source = new DataView(ds.Tables[0]);
            // Use the LiteralControl constructor to create a new
            // instance of the class.
            LiteralControl myLiteral = new LiteralControl();
            // Set the LiteralControl.Text property to an HTML
            // string and the TableName value of a data source.
            myLiteral.Text = "<h6><font face=verdana>Caching an XML Table: " + Source.Table.TableName + " </font></h6>";
            MyDataGrid.DataSource = Source;
            MyDataGrid.DataBind();

            TimeMsg.Text = DateTime.Now.ToString("G");
        }
    }
}
' Filename is partialcache.vb
' Create a code-behind user control that is cached
' for 20 seconds using the PartialCachingAttribute class.
' This control uses a DataGrid server control to display
' XML data.
Imports System.IO
Imports System.Data
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace Samples.AspNet.VB.Controls

    ' Set the PartialCachingAttribute.Duration property to 20 seconds.
    <PartialCaching(20)> _
    Partial Class ctlMine
        Inherits UserControl

        Protected Sub Page_Load(ByVal Src As [Object], ByVal E As EventArgs)
            Dim ds As New DataSet()

            Dim fs As New FileStream(Server.MapPath("schemadata.xml"), FileMode.Open, FileAccess.Read)
            Dim reader As New StreamReader(fs)
            ds.ReadXml(reader)
            fs.Close()

            Dim [Source] As New DataView(ds.Tables(0))
            ' Use the LiteralControl constructor to create a new
            ' instance of the class.
            Dim myLiteral As New LiteralControl()
            ' Set the LiteralControl.Text property to an HTML
            ' string and the TableName value of a data source.
            myLiteral.Text = "<h6><font face=verdana>Caching an XML Table: " & [Source].Table.TableName & " </font></h6>"
            MyDataGrid.DataSource = [Source]
            MyDataGrid.DataBind()

            TimeMsg.Text = DateTime.Now.ToString("G")
        End Sub
    End Class
End Namespace

範例的第二部分展示了與前一個範例一同使用的使用者控制項,用以展示使用者控制快取。

<!-- The mark-up .ascx file that displays the output of
     the partialcache.cs user control code-behind file. -->
<%@ Control language="C#" inherits="Samples.AspNet.CS.Controls.ctlMine" CodeFile="partialcache.cs.ascx.cs" %>

  <ASP:DataGrid id="MyDataGrid" runat="server"
    Width="900"
    BackColor="#ccccff"
    BorderColor="black"
    ShowFooter="false"
    CellPadding="3"
    CellSpacing="0"
    Font-Names="Verdana"
    Font-Size="8pt"
    HeaderStyle-BackColor="#aaaadd"
    EnableViewState="false"
  />

  <br />

  <i>Control last generated on:</i> <asp:label id="TimeMsg" runat="server" />
<!-- The mark-up .ascx file that displays the output of
     the partialcache.vb user control code-behind file. -->
<%@ Control language="vb" inherits="Samples.AspNet.VB.Controls.ctlMine" CodeFile="partialcache.vb.ascx.vb" %>

  <ASP:DataGrid id="MyDataGrid" runat="server"
    Width="900"
    BackColor="#ccccff"
    BorderColor="black"
    ShowFooter="false"
    CellPadding="3"
    CellSpacing="0"
    Font-Names="Verdana"
    Font-Size="8pt"
    HeaderStyle-BackColor="#aaaadd"
    EnableViewState="false"
  />

  <br />

  <i>Control last generated on:</i> <asp:label id="TimeMsg" runat="server" />

範例的第三部分展示了一個網頁表單頁面,該頁面承載使用者控制功能。

<!-- The WebForms page that contains the user control generated
     by partialcache.cs. -->
<%@ Register TagPrefix="Acme" TagName="Cache" Src="partialcache.cs.ascx" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<script language="C#" runat="server">

      void Page_Load(Object Src, EventArgs E ) {

          TimeMsg.Text = DateTime.Now.ToString("G");
      }

  </script>

<head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
  
  <form id="form1" runat="server">
    <Acme:Cache runat="server"/>
    <br />

    <i>Page last generated on:</i> <asp:label id="TimeMsg" runat="server" />

  </form>
</body>
</html>
<!-- The WebForms page that contains the user control generated
     by partialcache.vb. -->
<%@ Register TagPrefix="Acme" TagName="Cache" Src="partialcache.vb.ascx" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<script language="vb" runat="server">

   Sub Page_Load(Src As [Object], E As EventArgs) 
      TimeMsg.Text = DateTime.Now.ToString("G")
   End Sub 'Page_Load

  </script>

<head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
  
  <form id="form1" runat="server">
    <Acme:Cache runat="server"/>
    <br />

    <i>Page last generated on:</i> <asp:label id="TimeMsg" runat="server" />

  </form>
</body>
</html>

備註

PartialCachingAttribute屬性類別標示支援片段快取的使用者控制項(.ascx 檔案),並封裝 ASP.NET 在快取控制項時所使用的快取設定。 頁面與控制項開發者會使用該 PartialCachingAttribute 屬性來啟用使用者控制的輸出快取,並保留程式碼背後的檔案。

使用這個 PartialCachingAttribute 是啟用輸出快取的多種方法之一。 以下列表說明了啟用輸出快取的方法。

若使用者控制項包含 @ OutputCache 指令或套用 PartialCachingAttribute,ASP.NET 解析器會產生 PartialCachingControl 類別的實例以包裝使用者控制項。

欲了解更多關於 ASP.NET 快取的資訊,請參見 Caching。 欲了解更多屬性的使用資訊,請參閱屬性。

建構函式

名稱 Description
PartialCachingAttribute(Int32, String, String, String, Boolean)

初始化該類別的新實例 PartialCachingAttribute ,指定快取持續時間、任意 GET 值、 POST 控制名稱、自訂輸出快取需求以調整快取,以及使用者控制輸出是否能與多個頁面共享。

PartialCachingAttribute(Int32, String, String, String, String, Boolean)

初始化類別的新實例 PartialCachingAttribute ,指定快取持續時間、任何 GET 值、 POST 控制名稱、自訂輸出快取需求以調整快取、資料庫依賴性,以及使用者控制輸出是否能與多個頁面共享。

PartialCachingAttribute(Int32, String, String, String)

初始化該類別的新實例 PartialCachingAttribute ,指定快取持續時間、任何 GET 與 POST 值、控制名稱,以及用於調整快取的自訂輸出快取需求。

PartialCachingAttribute(Int32)

初始化一個新的類別實例 PartialCachingAttribute ,並指定使用者控制項的快取時間。

屬性

名稱 Description
Duration

讀取快取項目應該在輸出快取中停留的時間(以秒計)。

ProviderName

取得或設定用於儲存輸出快取資料的提供者名稱,該資料用於相關控制項。

Shared

會取得一個值,表示使用者控制輸出是否能與多個頁面共享。

SqlDependency

取得一個分隔字串,用以識別一個或多個資料庫與資料表名稱對,這些對是快取使用者控制所依賴的。

TypeId

在衍生類別中實作時,取得這個 Attribute的唯一標識碼。

(繼承來源 Attribute)
VaryByControls

會取得一份使用者控制屬性清單,輸出快取用來調整使用者控制。

VaryByCustom

會取得一串自訂字串,輸出快取會用來調整使用者控制。

VaryByParams

會獲得一份查詢字串或表單 POST 參數清單,輸出快取會用來調整使用者控制。

方法

名稱 Description
Equals(Object)

傳回值,這個值表示這個實例是否等於指定的物件。

(繼承來源 Attribute)
GetHashCode()

傳回這個實例的哈希碼。

(繼承來源 Attribute)
GetType()

取得目前實例的 Type

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,指出這個實例的值是否為衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,傳回值,指出這個實例是否等於指定的物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)

明確介面實作

名稱 Description
_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取 物件的型別資訊,可用來取得介面的類型資訊。

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開屬性和方法的存取權。

(繼承來源 Attribute)

適用於

另請參閱