BasePartialCachingControl.CachePolicy 属性
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取与预包装用户控件关联的 ControlCachePolicy 对象。
public:
property System::Web::UI::ControlCachePolicy ^ CachePolicy { System::Web::UI::ControlCachePolicy ^ get(); };
public System.Web.UI.ControlCachePolicy CachePolicy { get; }
member this.CachePolicy : System.Web.UI.ControlCachePolicy
Public ReadOnly Property CachePolicy As ControlCachePolicy
一个 ControlCachePolicy,存储与预包装用户控件的输出缓存相关的属性。
下面的代码示例演示了如何在运行时以编程方式动态加载用户控件和以编程方式操作。 此示例有三个部分:
一个分部类,
LogOnControl
它继承自 UserControl 基类和应用属性 PartialCachingAttribute 的基类。与
LogOnControl
分部类一起使用的用户控件。承载用户控件的Web Forms页。
若要成功运行此示例,请确保用户控制文件 (.ascx) 、其代码隐藏文件 (.cs 或 .vb) ,以及承载用户控件 (.aspx) 的Web Forms页位于同一目录中。
本示例的第一部分演示了如何将该 PartialCachingAttribute 控件应用于名为 LogOnControl
的用户控件,这意味着用户控件在运行时由 PartialCachingControl 控件包装。 LogOnControl
可以通过对象的关联ControlCachePolicy对象以编程方式操作对象的缓存设置,该对象可通过对PartialCachingControl包装它的引用来使用。 在此示例中,在页面初始化期间检查缓存设置,如果满足某些条件,则更改。
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
[PartialCaching(100)]
public class LogOnControl:UserControl
{
public TextBox user;
public TextBox password;
}
Imports System.Web.UI
Imports System.Web.UI.WebControls
<PartialCaching(100)> _
Public Class LogOnControl
Inherits UserControl
Public user As TextBox
Public password As TextBox
End Class
本示例的第二部分显示了与上一个示例一起使用的用户控件,用于演示用户控件缓存。
<%@ control inherits = "LogOnControl" src = "LogOnControl.cs" %>
<!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 style="font: 10pt verdana;border-width:1;border-style:solid;border-color:black;" cellspacing="15">
<tr>
<td><b>Login: </b></td>
<td><asp:TextBox id="user" runat="server"/></td>
</tr>
<tr>
<td><b>Password: </b></td>
<td><asp:TextBox id="password" TextMode="Password" runat="server"/></td>
</tr>
<tr>
</tr>
</table>
</form>
</body>
</html>
<%@ control inherits = "LogOnControl" src = "LogOnControl.vb" %>
<!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 style="font: 10pt verdana;border-width:1;border-style:solid;border-color:black;" cellspacing="15">
<tr>
<td><b>Login: </b></td>
<td><ASP:TextBox id="user" runat="server"/></td>
</tr>
<tr>
<td><b>Password: </b></td>
<td><ASP:TextBox id="password" TextMode="Password" runat="server"/></td>
</tr>
<tr>
</tr>
</table>
</form>
</body>
</html>
本示例的第三部分演示了如何通过Web Forms页使用LogOnControl
用户控件。
<%@ Page Language="C#" Debug = "true"%>
<%@ Reference Control="Logonformcs.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 "Logonform.ascx" and "LogOnControl.cs" 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("Logonform.cs.ascx") as PartialCachingControl;
ControlCachePolicy cacheSettings = pcc.CachePolicy;
// If the control is slated to expire in greater than 60 Seconds
if (cacheSettings.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.
cacheSettings.SetExpires(DateTime.Now.Add(TimeSpan.FromSeconds(30)));
cacheSettings.SetSlidingExpiration(false);
}
Controls.Add(pcc);
}
</script>
<%@ Page Language="VB" Debug = "true"%>
<%@ Reference Control="Logonformvb.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 "Logonform.ascx" and "LogOnControl.vb" file in
' the same directory as the aspx file.
Sub Page_Init(ByVal Sender As Object, ByVal e As EventArgs)
' Obtain a PartialCachingControl object which wraps the 'LogOnControl' user control.
Dim pcc As PartialCachingControl
pcc = CType(LoadControl("Logonform.vb.ascx"), PartialCachingControl)
Dim cacheSettings As ControlCachePolicy
cacheSettings = pcc.CachePolicy
' If the control is slated to expire in greater than 60 Seconds
If (cacheSettings.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.
cacheSettings.SetExpires(DateTime.Now.Add(TimeSpan.FromSeconds(30)))
cacheSettings.SetSlidingExpiration(False)
End If
Controls.Add(pcc)
End Sub ' Page_Init
</script>
该 CachePolicy 属性允许以编程方式访问 ControlCachePolicy 与实例包含 BasePartialCachingControl 的用户控件关联的对象。 可以编程方式操作对象 ControlCachePolicy ,以影响用户控件的缓存行为和设置。