PartialCachingAttribute 类

定义

定义 Web 窗体用户控件(.ascx 文件)用于指示是否以及如何缓存其输出的元数据特性。Defines the metadata attribute that Web Forms user controls (.ascx files) use to indicate if and how their output is cached. 此类不能被继承。This class cannot be inherited.

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

示例

下面的代码示例演示如何使用PartialCachingAttributeThe following code example demonstrates using the PartialCachingAttribute. 此示例包含三个部分:This example has three parts:

  • 一个分部类ctlMine,它继承UserControl自基类并应用该PartialCachingAttribute特性。A partial class, ctlMine, that inherits from the UserControl base class and to which the PartialCachingAttribute attribute is applied.

  • ctlMine分部类一起使用的用户控件。A user control that is used with the ctlMine partial class.

  • 承载用户控件的 Web 窗体页。A Web Forms page that hosts the user control.

该示例的第一部分演示了从UserControl基类继承的分部类,以及应用属性的PartialCachingAttribute类。The first part of the example demonstrates a partial class that inherits from the UserControl base class and to which the PartialCachingAttribute attribute is applied. 在此示例中,属性指定应将用户控件缓存20秒。In this example, the attribute specifies that the user control should be cached for 20 seconds.

// [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 second part of the example shows a user control that is used with the previous example to demonstrate user control caching.

<!-- 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 窗体页。The third part of the example demonstrates a Web Forms page that hosts the user control.

<!-- 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 使用的缓存设置。The PartialCachingAttribute attribute class marks user controls (.ascx files) that support fragment caching, and encapsulates the cache settings that ASP.NET uses when caching the control. 页面和控件开发人员使用PartialCachingAttribute特性在代码隐藏文件中启用用户控件的输出缓存。Page and controls developers use the PartialCachingAttribute attribute to enable output caching for a user control in a code-behind file.

PartialCachingAttribute使用可以启用输出缓存的多种方式之一。Using the PartialCachingAttribute is one of several ways you can enable output caching. 以下列表描述了可用于启用输出缓存的方法。The following list describes methods you can use to enable output caching.

如果用户控件包含指令或已PartialCachingAttribute应用,则 ASP.NET 分析器会生成PartialCachingControl类的实例以包装用户控件。If a user control contains an directive or has a PartialCachingAttribute applied, the ASP.NET parser generates an instance of the PartialCachingControl class to wrap the user control.

有关 ASP.NET 缓存的详细信息,请参阅缓存For more information about ASP.NET caching, see Caching. 有关使用特性的详细信息,请参阅特性For more information about using attributes, see Attributes.

构造函数

PartialCachingAttribute(Int32)

使用分配给要缓存的用户控件的指定持续时间初始化 PartialCachingAttribute 类的新实例。Initializes a new instance of the PartialCachingAttribute class with the specified duration assigned to the user control to be cached.

PartialCachingAttribute(Int32, String, String, String)

初始化 PartialCachingAttribute 类的新实例,用于指定缓存持续时间、任何 GET 和 POST 值、控件名和用于改变缓存的自定义输出缓存要求。Initializes a new instance of the PartialCachingAttribute class, specifying the caching duration, any GET and POST values, control names, and custom output caching requirements used to vary the cache.

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

初始化 PartialCachingAttribute 类的新实例,指定缓存持续时间、所有 GETPOST 值、控件名、用于改变缓存的自定义输出缓存要求以及用户控件输出是否可在多页间共享。Initializes a new instance of the PartialCachingAttribute class, specifying the caching duration, any GET and POST values, control names, custom output caching requirements used to vary the cache, and whether the user control output can be shared with multiple pages.

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

初始化 PartialCachingAttribute 类的新实例,指定缓存持续时间、所有 GETPOST 值、控件名、用于改变缓存的自定义输出缓存要求、数据库依赖项以及用户控件输出是否可在多页间共享。Initializes a new instance of the PartialCachingAttribute class, specifying the caching duration, any GET and POST values, control names, custom output caching requirements used to vary the cache, the database dependencies, and whether the user control output can be shared with multiple pages.

属性

Duration

获取缓存项应保留在输出缓存中的时间(以秒为单位)。Gets the amount of time, in seconds, that cached items should remain in the output cache.

ProviderName

获取或设置提供程序的名称,该提供程序用于存储关联控件的输出缓存数据。Gets or sets the name of the provider that is used to store the output-cached data for the associated control.

Shared

获取一个值,该值指示用户控件输出是否可在多页间共享。Gets a value indicating whether user control output can be shared with multiple pages.

SqlDependency

获取一个分隔字符串,该字符串标识用户控件所依赖的一个或多个数据库和表的名称对。Gets a delimited string that identifies one or more database and table name pairs that the cached user control is dependent on.

TypeId

在派生类中实现时,获取此 Attribute 的唯一标识符。When implemented in a derived class, gets a unique identifier for this Attribute.

(继承自 Attribute)
VaryByControls

获取输出缓存用于使用户控件发生变化的用户控件属性列表。Gets a list of user control properties that the output cache uses to vary the user control.

VaryByCustom

获取输出缓存将用于改变用户控件的自定义字符串列表。Gets a list of custom strings that the output cache will use to vary the user control.

VaryByParams

获取输出缓存将用于改变用户控件的查询字符串或窗体 POST 参数的列表。Gets a list of query string or form POST parameters that the output cache will use to vary the user control.

方法

Equals(Object)

返回一个值,该值指示此实例是否与指定的对象相等。Returns a value that indicates whether this instance is equal to a specified object.

(继承自 Attribute)
GetHashCode()

返回此实例的哈希代码。Returns the hash code for this instance.

(继承自 Attribute)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
IsDefaultAttribute()

在派生类中重写时,指示此实例的值是否是派生类的默认值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(继承自 Attribute)
Match(Object)

当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(继承自 Attribute)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)

显式接口实现

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

将一组名称映射为对应的一组调度标识符。Maps a set of names to a corresponding set of dispatch identifiers.

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

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。Retrieves the type information for an object, which can be used to get the type information for an interface.

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

检索对象提供的类型信息接口的数量(0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

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

提供对某一对象公开的属性和方法的访问。Provides access to properties and methods exposed by an object.

(继承自 Attribute)

适用于

另请参阅