ControlCachePolicy.SetExpires(DateTime) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
指示包裝使用者控制項的 BasePartialCachingControl 控制項,讓快取項目在指定日期和時間到期。
public:
void SetExpires(DateTime expirationTime);
public void SetExpires (DateTime expirationTime);
member this.SetExpires : DateTime -> unit
Public Sub SetExpires (expirationTime As DateTime)
參數
例外狀況
使用者控制項與 BasePartialCachingControl 沒有關聯,也不能快取。
範例
下列程式碼範例示範如何在執行時間以程式設計方式動態載入使用者控制項。 屬性 PartialCachingAttribute 會套用至名為 SimpleControl
的使用者控制項,這表示使用者控制項會在執行時間由 PartialCachingControl 控制項包裝。 物件的 SimpleControl
快取設定可以透過其相關聯的 ControlCachePolicy 物件以程式設計方式操作,該物件可透過包裝它的控制項參考 PartialCachingControl 取得。 在此範例中,如果符合某些條件,則會 Duration 在頁面初始化期間檢查 屬性, SetSlidingExpiration 並使用 和 SetExpires 方法進行變更。 這個範例是提供給 類別之較大範例的 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
) 來指定絕對到期原則。