共用方式為


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) 來指定絕對到期原則。

適用於

另請參閱