PartialCachingAttribute 類別

定義

定義中繼資料屬性,Web Form 使用者控制項 (.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 使用的使用者控制項。

  • 裝載使用者控制項的Web Form頁面。

此範例的第一個部分示範繼承自 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" />

範例的第三個部分示範裝載使用者控制項的 Web Form 頁面。

<!-- 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 快取的詳細資訊,請參閱快取。 如需使用屬性的詳細資訊,請參閱 屬性

建構函式

PartialCachingAttribute(Int32)

使用指派給要快取的使用者控制項持續期間,初始化 PartialCachingAttribute 類別的新執行個體。

PartialCachingAttribute(Int32, String, String, String)

初始化 PartialCachingAttribute 類別的新執行個體,並指定快取期間、任何的 GET 和 POST 值、控制項名稱以及用來變更快取的自訂輸出快取需求。

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

初始化 PartialCachingAttribute 類別的新執行個體,並指定快取期間、任何的 GETPOST 值、控制項名稱、用來變更快取的自訂輸出快取需求,以及使用者控制項輸出是否可以與多個頁面共用。

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

初始化 PartialCachingAttribute 類別的新執行個體,並指定快取期間、任何的 GETPOST 值、控制項名稱、用來變更快取的自訂輸出快取需求、資料庫相依性,以及使用者控制項輸出是否可以與多個頁面共用。

屬性

Duration

取得快取項目應停留在輸出快取區中的時間 (以秒為單位)。

ProviderName

取得或設定提供者名稱,這個名稱用來儲存關聯控制項的輸出快取資料。

Shared

取得值,表示使用者控制項輸出是否可與多個頁面共用。

SqlDependency

取得分隔的字串,以識別快取的使用者控制項所相依的一或多個資料庫和表格名稱組。

TypeId

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

(繼承來源 Attribute)
VaryByControls

取得輸出快取用來變更使用者控制項的使用者控制項屬性清單。

VaryByCustom

取得自訂字串的清單,輸出快取將使用此清單來變更使用者控制項。

VaryByParams

取得查詢字串的清單,或是輸出快取將用於變更使用者控制項的表單 POST 參數。

方法

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。

(繼承來源 Attribute)
GetHashCode()

傳回這個執行個體的雜湊碼。

(繼承來源 Attribute)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

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

(繼承來源 Object)

明確介面實作

_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)

適用於

另請參閱