ControlCachePolicy.SetExpires(DateTime) 方法
定义
指示包装用户控件的 BasePartialCachingControl 控件在指定的日期和时间使缓存项过期。Instructs the BasePartialCachingControl control that wraps the user control to expire the cache entry at the specified date and time.
public:
void SetExpires(DateTime expirationTime);
public void SetExpires (DateTime expirationTime);
member this.SetExpires : DateTime -> unit
Public Sub SetExpires (expirationTime As DateTime)
参数
例外
用户控件与 BasePartialCachingControl 没有关联,因此不可缓存。The user control is not associated with a BasePartialCachingControl and is not cacheable.
示例
下面的代码示例演示如何在运行时动态加载用户控件并以编程方式进行操作。The following code example demonstrates how a user control can be loaded dynamically and manipulated programmatically at run time. PartialCachingAttribute特性应用于名为的用户控件 SimpleControl ,这意味着用户控件 PartialCachingControl 在运行时由控件包装。The PartialCachingAttribute attribute is applied to a user control named SimpleControl, which means the user control is wrapped by a PartialCachingControl control at run time. SimpleControl对象的缓存设置可以通过其关联的对象进行编程操作 ControlCachePolicy ,该对象可通过对包装该对象的控件的引用提供 PartialCachingControl 。The SimpleControl object's caching settings can be programmatically manipulated through its associated ControlCachePolicy object, which is available through a reference to the PartialCachingControl control that wraps it. 在此示例中,在 Duration 页初始化期间检查属性,并在 SetSlidingExpiration SetExpires 满足某些条件时使用和方法进行更改。In this example, the Duration property is examined during page initialization and changed using the SetSlidingExpiration and SetExpires methods if some conditions are met. 此示例是为类提供的更大示例的一部分 ControlCachePolicy 。This example is part of a larger example provided for the ControlCachePolicy class.
<%@ 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>
注解
使用 SetExpires 和 SetSlidingExpiration 方法 (传递 true) ,以指示 BasePartialCachingControl 包装用户控件的控件使用可调过期缓存策略,而不是绝对过期策略。Use the SetExpires and SetSlidingExpiration methods (passing true) to instruct the BasePartialCachingControl control that wraps the user control to use a sliding expiration caching policy instead of an absolute expiration policy. 使用 SetExpires 方法和 SetSlidingExpiration 方法 (传递 false) 来指定绝对过期策略。Use the SetExpires method and the SetSlidingExpiration method (passing false) to specify an absolute expiration policy.