ControlCachePolicy.SetSlidingExpiration(Boolean) 方法

定义

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

public:
 void SetSlidingExpiration(bool useSlidingExpiration);
public void SetSlidingExpiration (bool useSlidingExpiration);
member this.SetSlidingExpiration : bool -> unit
Public Sub SetSlidingExpiration (useSlidingExpiration As Boolean)

参数

useSlidingExpiration
Boolean

如果使用可调缓存过期而不是绝对过期,则为 true;否则为 false

例外

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

示例

下面的代码示例演示如何在运行时动态加载和以编程方式操作用户控件。 名为 SimpleControl 的用户控件使用 PartialCachingAttribute 属性进行修饰,这意味着它在运行时由 控件 PartialCachingControl 包装。 SimpleControl对象的缓存设置可以通过其关联的ControlCachePolicy对象以编程方式操作,该对象可通过对包装它的控件的引用PartialCachingControl获得。 在此示例中,在页面初始化期间检查缓存设置,如果满足某些条件,则会更改这些设置。 此示例是为 类提供的更大示例的一 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) 指定绝对过期策略。

适用于

另请参阅