共用方式為


ControlCachePolicy.Duration 屬性

定義

取得或設定快取項目保留在輸出快取中的時間。

public:
 property TimeSpan Duration { TimeSpan get(); void set(TimeSpan value); };
public TimeSpan Duration { get; set; }
member this.Duration : TimeSpan with get, set
Public Property Duration As TimeSpan

屬性值

TimeSpan,表示使用者控制項保留在輸出快取中的時間。 預設為 Zero

例外狀況

使用者控制項與 BasePartialCachingControl 沒有關聯,也不能快取。

-或-

Duration 屬性是在控制項的初始化和呈現階段以外設定的。

範例

下列程式碼範例示範如何在執行時間動態載入使用者控制項及以程式設計方式操作。 屬性 PartialCachingAttribute 會套用至名為 SimpleControl 的使用者控制項,這表示它會在執行時間由 PartialCachingControl 控制項包裝。 物件的 SimpleControl 快取設定可以透過其相關聯的 ControlCachePolicy 物件以程式設計方式操作,其可透過包裝它的控制項參考 PartialCachingControl 取得。 在此範例中,如果符合某些條件, Duration 則會在頁面初始化期間檢查 屬性,並變更快取到期時間。 此範例是針對 類別提供的較大範例的 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>

備註

如果使用 方法設定 SetSlidingExpiration 絕對到期原則, Duration 則 屬性會傳回剩餘的時間量,直到快取專案到期為止。

適用於

另請參閱