PartialCachingAttribute 类

定义

定义 Web 窗体用户控件(.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 Forms页。

本示例的第一部分演示了从 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 Forms页。

<!-- 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。 有关使用属性的详细信息,请参阅 “属性”。

构造函数

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)

将一组名称映射为对应的一组调度标识符。

(继承自 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。

(继承自 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。

(继承自 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。

(继承自 Attribute)

适用于

另请参阅