更新:2007 年 11 月
定义 Web 窗体用户控件(.ascx 文件)用于指示是否以及如何缓存其输出的元数据属性。无法继承此类。
命名空间:
System.Web.UI 程序集:
System.Web(在 System.Web.dll 中)
<AttributeUsageAttribute(AttributeTargets.Class)> _
<AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
Public NotInheritable Class PartialCachingAttribute _
Inherits Attribute
Dim instance As PartialCachingAttribute
[AttributeUsageAttribute(AttributeTargets.Class)]
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public sealed class PartialCachingAttribute : Attribute
[AttributeUsageAttribute(AttributeTargets::Class)]
[AspNetHostingPermissionAttribute(SecurityAction::LinkDemand, Level = AspNetHostingPermissionLevel::Minimal)]
public ref class PartialCachingAttribute sealed : public Attribute
/** @attribute AttributeUsageAttribute(AttributeTargets.Class) */
/** @attribute AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal) */
public final class PartialCachingAttribute extends Attribute
public final class PartialCachingAttribute extends Attribute
PartialCachingAttribute 属性类标记用户控件(.ascx 文件),这些用户控件支持片段缓存,并封装了 ASP.NET 缓存控件时使用的缓存设置。页和控件开发人员使用 PartialCachingAttribute 属性在代码隐藏文件中启用用户控件的输出缓存。
使用 PartialCachingAttribute 是可启用输出缓存的几种方法之一。下表介绍可用来启用输出缓存的方法。
如果用户控件包含 @ OutputCache 指令或应用了 PartialCachingAttribute,则 ASP.NET 分析器将生成 PartialCachingControl 类的实例来包装该用户控件。
有关 ASP.NET 缓存的更多信息,请参见 ASP.NET 缓存。有关使用属性的更多信息,请参见 利用属性扩展元数据。
下面的代码示例演示如何使用 PartialCachingAttribute。此示例包括三个部分:
示例的第一部分演示一个分部类,该分部类继承自 UserControl 基类,并且应用了 PartialCachingAttribute 属性。在此示例中,该属性指定用户控件将缓存 20 秒。
' 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
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 'Page_Load
End Class 'ctlMine
End Namespace
// [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 partialcache.jsl]
// 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.
import System.*;
import System.IO.*;
import System.Data.*;
import System.Web.*;
import System.Web.UI.*;
import System.Web.UI.WebControls.*;
// Set the PartialCachingAttribute.Duration property to 20 seconds.
/** @attribute PartialCaching(20)
*/
public class ctlMine extends UserControl
{
public DataGrid myDataGrid;
public Label timeMsg;
protected void Page_Load(Object src, EventArgs e)
{
DataSet ds = new DataSet();
FileStream fs = new FileStream(get_Server().MapPath("schemadata.xml"),
FileMode.Open, FileAccess.Read);
StreamReader reader = new StreamReader(fs);
ds.ReadXml(reader);
fs.Close();
DataView source = new DataView(ds.get_Tables().get_Item(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.set_Text("<h6><font face=verdana>Caching an XML Table: "
+ source.get_Table().get_TableName() + " </font></h6>");
myDataGrid.set_DataSource(source);
myDataGrid.DataBind();
timeMsg.set_Text(DateTime.get_Now().ToString("G"));
} //Page_Load
} //ctlMine
示例的第二部分显示一个用户控件,该控件在前面的示例中用来演示用户控件缓存。
<!-- 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 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.jsl user control code-behind file.
<%@ Control language="VJ#" inherits="ctlMine" src="partialcache.jsl" %>
<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 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 >
<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>
<!-- 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 >
<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.jsl.
<%@ Register TagPrefix="Acme" TagName="Cache" Src="partialcache.jsl.ascx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<script language="VJ#" runat="server">
void Page_Load(Object src, EventArgs e )
{
TimeMsg.set_Text(DateTime.get_Now().ToString("G"));
} //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>
System..::.Object
System..::.Attribute
System.Web.UI..::.PartialCachingAttribute
此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。
Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
.NET Framework 和 .NET Compact Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。
.NET Framework
受以下版本支持:3.5、3.0、2.0、1.1、1.0
参考
其他资源