ControlCachePolicy 类

定义

提供对 ASP.NET 用户控件的输出缓存设置的编程访问。

public ref class ControlCachePolicy sealed
public sealed class ControlCachePolicy
type ControlCachePolicy = class
Public NotInheritable Class ControlCachePolicy
继承
ControlCachePolicy

示例

下面的代码示例演示了如何在运行时以编程方式动态加载用户控件。 特性 PartialCachingAttribute 应用于名为 SimpleControl的用户控件,这意味着用户控件在运行时由控件 PartialCachingControl 包装。 对象的 SimpleControl 缓存设置可以通过其关联的 ControlCachePolicy 对象以编程方式操作,该对象可通过对包装对象的控件的 PartialCachingControl 引用获得。 在此示例中,在 Duration 页面初始化期间检查 属性,如果满足某些条件,则使用 SetSlidingExpirationSetExpires 方法进行更改。

<%@ Page Language="C#" %>
<%@ Reference Control="SimpleControl.ascx" %>
<script language="C#" runat="server">

// The following example demonstrates how to load a user control dynamically at run time, and
// work with the ControlCachePolicy object associated with it.

// Loads and displays a UserControl defined in a seperate Logonform.ascx file.
// You need to have "SimpleControl.ascx" file in 
// the same directory as the aspx file. 

void Page_Init(object sender, System.EventArgs e) {
    
    // Obtain a PartialCachingControl object which wraps the 'LogOnControl' user control.
    PartialCachingControl pcc = LoadControl("SimpleControl.ascx") as PartialCachingControl;        
    
    // If the control is slated to expire in greater than 60 Seconds
    if (pcc.CachePolicy.Duration > TimeSpan.FromSeconds(60) ) 
    {        
        // Make it expire faster. Set a new expiration time to 30 seconds, and make it
        // an absolute expiration if it isnt already.        
        pcc.CachePolicy.SetExpires(DateTime.Now.Add(TimeSpan.FromSeconds(30)));
        pcc.CachePolicy.SetSlidingExpiration(false);
    }                    
    Controls.Add(pcc);
}
</script>
<%@ Page Language="VB" %>
<%@ Reference Control="SimpleControl.ascx" %>
<script language="VB" runat="server">

    ' The following example demonstrates how to load a user control dynamically at run time, and
    ' work with the ControlCachePolicy object associated with it.

    ' Loads and displays a UserControl defined in a seperate Logonform.ascx file.
    ' You need to have "SimpleControl.ascx" file in 
    ' the same directory as the aspx file. 

    Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs)
    
        ' Obtain a PartialCachingControl object which wraps the 'LogOnControl' user control.
        Dim pcc As PartialCachingControl
        pcc = LoadControl("SimpleControl.ascx")
    
        ' If the control is slated to expire in greater than 60 Seconds
        If (pcc.CachePolicy.Duration > TimeSpan.FromSeconds(60)) Then
            ' Make it expire faster. Set a new expiration time to 30 seconds, and make it
            ' an absolute expiration if it isnt already.        
            pcc.CachePolicy.SetExpires(DateTime.Now.Add(TimeSpan.FromSeconds(30)))
            pcc.CachePolicy.SetSlidingExpiration(False)
        End If
        Controls.Add(pcc)
    End Sub
</script>

下面的代码示例演示如何从Web Forms页使用SimpleControl用户控件。 若要成功运行此示例,请确保用户控件文件 (.ascx) 、其代码隐藏文件 (.cs 或 .vb) ,以及承载用户控件 (.aspx) 的Web Forms页。

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

[PartialCaching(100)]
public partial class SimpleControl : System.Web.UI.UserControl
{    
    protected void Page_Load(object sender, EventArgs e)
    {
        ItemsRemaining.Text = GetAvailableItems().ToString();
        CacheTime.Text = DateTime.Now.ToLongTimeString();
    }

    private int GetAvailableItems()
    {
        SqlConnection sqlConnection = new SqlConnection
            ("Initial Catalog=Northwind;Data Source=localhost;Integrated Security=SSPI;");
        SqlCommand sqlCommand = sqlConnection.CreateCommand();
        sqlCommand.CommandType = CommandType.StoredProcedure;
        sqlCommand.CommandText = "GetRemainingItems";
        sqlConnection.Open();
        int items = (int)sqlCommand.ExecuteScalar();
        sqlConnection.Close();
        return items;
    }
}
Imports System.Data.SqlClient

Partial Class SimpleControl
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        ItemsRemaining.Text = GetAvailableItems().ToString()
        CacheTime.Text = DateTime.Now.ToLongTimeString()
    End Sub

    Private Function GetAvailableItems() As Integer
        Dim sqlConnection As SqlConnection
        Dim sqlCommand As SqlCommand
        Dim items As Integer

        sqlConnection = New SqlConnection("Initial Catalog=Northwind;Data Source=localhost;Integrated Security=SSPI;")
        sqlCommand = sqlConnection.CreateCommand()
        sqlCommand.CommandType = Data.CommandType.StoredProcedure
        sqlCommand.CommandText = "GetRemainingItems"
        sqlConnection.Open()
        items = CInt(sqlCommand.ExecuteScalar())
        sqlConnection.Close()
        Return items
    End Function
End Class
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="SimpleControl.ascx.cs" Inherits="SimpleControl" %>
<!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">
<head id="Head1" runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
<form id="Form1" runat="server">
<table cellspacing="15">
<tr>
<td><b>Available items: </b></td>
<td>
    <asp:Label ID="ItemsRemaining" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td><b>As of: </b></td>
<td>
    <asp:Label ID="CacheTime" runat="server" Text="Label"></asp:Label>
</td>
</tr>
</table>
</form>
</body>
</html>
<%@ Control Language="VB" AutoEventWireup="true" CodeFile="SimpleControl.ascx.vb" Inherits="SimpleControl" %>
<!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">
<head id="Head1" runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
<form id="Form1" runat="server">
<table cellspacing="15">
<tr>
<td><b>Available items: </b></td>
<td>
    <asp:Label ID="ItemsRemaining" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td><b>As of: </b></td>
<td>
    <asp:Label ID="CacheTime" runat="server" Text="Label"></asp:Label>
</td>
</tr>
</table>
</form>
</body>
</html>

注解

ControlCachePolicy开发人员在编程用户控件方案中使用 类来指定用户控件 (.ascx 文件) 的输出缓存设置。 ASP.NET 在实例中 BasePartialCachingControl 嵌入用户控件。 类 BasePartialCachingControl 表示启用了输出缓存的用户控件。 访问 BasePartialCachingControl.CachePolicy 控件的 PartialCachingControl 属性时,始终会收到有效的 ControlCachePolicy 对象。 但是,如果访问UserControl.CachePolicy控件UserControl的 属性,则仅当用户控件已由控件BasePartialCachingControl包装时,才会收到有效的ControlCachePolicy对象。 如果未包装该属性,则在 ControlCachePolicy 尝试操作该对象时,属性返回的对象将引发异常,因为它没有关联的 BasePartialCachingControl。 若要确定实例是否 UserControl 支持不生成异常的缓存,请检查 SupportsCaching 属性。

ControlCachePolicy使用 类是启用输出缓存的几种方法之一。 以下列表介绍了可用于启用输出缓存的方法:

  • @ OutputCache使用 指令在声明性方案中启用输出缓存。

  • PartialCachingAttribute使用 特性为代码隐藏文件中的用户控件启用缓存。

  • ControlCachePolicy在编程方案中,使用 类指定缓存设置,在这种情况下,BasePartialCachingControl使用上述方法之一启用了缓存并使用 方法动态加载的TemplateControl.LoadControl实例。 ControlCachePolicy只能在控件生命周期的 和 PreRender 阶段之间Init成功操作实例。 如果在阶段后PreRender修改ControlCachePolicy对象,ASP.NET 将引发异常,因为控件呈现后所做的任何更改实际上都不会影响缓存设置, (在阶段) 期间Render缓存控件。 最后,用户控件实例 (,因此其 ControlCachePolicy 对象) 仅在实际呈现时可用于编程操作。

属性

Cached

获取或设置一个值,该值指示是否为用户控件启用片段缓存。

Dependency

获取或设置与缓存的用户控件输出关联的 CacheDependency 类的实例。

Duration

获取或设置缓存的项将在输出缓存中保留的时间。

ProviderName

获取或设置与控件实例关联的输出缓存提供程序的名称,。

SupportsCaching

获取一个值,该值指示用户控件是否支持缓存。

VaryByControl

获取或设置要用来改变缓存输出的控件标识符列表。

VaryByParams

获取或设置要用来改变缓存输出的 GETPOST 参数名称列表。

方法

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
SetExpires(DateTime)

指示包装用户控件的 BasePartialCachingControl 控件在指定的日期和时间使缓存项过期。

SetSlidingExpiration(Boolean)

指示包装用户控件的 BasePartialCachingControl 控件将用户控件的缓存项设置为使用可调或绝对过期。

SetVaryByCustom(String)

设置要由输出缓存用来改变用户控件的自定义字符串列表。

ToString()

返回表示当前对象的字符串。

(继承自 Object)

适用于

另请参阅