ControlCachePolicy.SetExpires(DateTime) 方法

定义

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

public:
 void SetExpires(DateTime expirationTime);
public void SetExpires (DateTime expirationTime);
member this.SetExpires : DateTime -> unit
Public Sub SetExpires (expirationTime As DateTime)

参数

expirationTime
DateTime

DateTime,之后缓存项会过期。

例外

用户控件与 BasePartialCachingControl 没有关联,因此不可缓存。

示例

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

<%@ 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包装用户控件的控件使用滑动过期缓存策略而不是绝对过期策略。 SetExpires使用该方法和SetSlidingExpiration方法 (传递false) 来指定绝对过期策略。

适用于

另请参阅