ControlCachePolicy 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供對 ASP.NET 使用者控制項輸出快取設定的程式設計存取。
public ref class ControlCachePolicy sealed
public sealed class ControlCachePolicy
type ControlCachePolicy = class
Public NotInheritable Class ControlCachePolicy
- 繼承
-
ControlCachePolicy
範例
下列程式碼範例示範如何在執行時間以程式設計方式動態載入使用者控制項。 屬性 PartialCachingAttribute 會套用至名為 SimpleControl
的使用者控制項,這表示使用者控制項會在執行時間由 PartialCachingControl 控制項包裝。 物件的 SimpleControl
快取設定可以透過其相關聯的 ControlCachePolicy 物件以程式設計方式操作,該物件可透過包裝它的控制項參考 PartialCachingControl 取得。 在此範例中,如果符合某些條件,則會 Duration 在頁面初始化期間檢查 屬性, SetSlidingExpiration 並使用 和 SetExpires 方法進行變更。
<%@ 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>
下列程式碼範例示範如何從Web Form頁面使用 SimpleControl
使用者控制項。 若要成功執行此範例,請確定使用者控制項檔案 (.ascx) 、其程式碼後置檔案 (.cs 或 .vb) ,以及裝載使用者控制項 (.aspx) 的 Web Form 頁面位於相同的目錄中。
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
[PartialCaching(100)]
public partial class SimpleControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
ItemsRemaining.Text = GetAvailableItems().ToString();
CacheTime.Text = DateTime.Now.ToLongTimeString();
}
private int GetAvailableItems()
{
SqlConnection sqlConnection = new SqlConnection
("Initial Catalog=Northwind;Data Source=localhost;Integrated Security=SSPI;");
SqlCommand sqlCommand = sqlConnection.CreateCommand();
sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand.CommandText = "GetRemainingItems";
sqlConnection.Open();
int items = (int)sqlCommand.ExecuteScalar();
sqlConnection.Close();
return items;
}
}
Imports System.Data.SqlClient
Partial Class SimpleControl
Inherits System.Web.UI.UserControl
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
ItemsRemaining.Text = GetAvailableItems().ToString()
CacheTime.Text = DateTime.Now.ToLongTimeString()
End Sub
Private Function GetAvailableItems() As Integer
Dim sqlConnection As SqlConnection
Dim sqlCommand As SqlCommand
Dim items As Integer
sqlConnection = New SqlConnection("Initial Catalog=Northwind;Data Source=localhost;Integrated Security=SSPI;")
sqlCommand = sqlConnection.CreateCommand()
sqlCommand.CommandType = Data.CommandType.StoredProcedure
sqlCommand.CommandText = "GetRemainingItems"
sqlConnection.Open()
items = CInt(sqlCommand.ExecuteScalar())
sqlConnection.Close()
Return items
End Function
End Class
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="SimpleControl.ascx.cs" Inherits="SimpleControl" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="Form1" runat="server">
<table cellspacing="15">
<tr>
<td><b>Available items: </b></td>
<td>
<asp:Label ID="ItemsRemaining" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td><b>As of: </b></td>
<td>
<asp:Label ID="CacheTime" runat="server" Text="Label"></asp:Label>
</td>
</tr>
</table>
</form>
</body>
</html>
<%@ Control Language="VB" AutoEventWireup="true" CodeFile="SimpleControl.ascx.vb" Inherits="SimpleControl" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="Form1" runat="server">
<table cellspacing="15">
<tr>
<td><b>Available items: </b></td>
<td>
<asp:Label ID="ItemsRemaining" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td><b>As of: </b></td>
<td>
<asp:Label ID="CacheTime" runat="server" Text="Label"></asp:Label>
</td>
</tr>
</table>
</form>
</body>
</html>
備註
開發人員會在程式設計使用者控制項案例中使用 類別 ControlCachePolicy ,以指定使用者控制項的輸出快取設定, (.ascx 檔案) 。 ASP.NET 在 實例內 BasePartialCachingControl 嵌使用者控制項。 類別 BasePartialCachingControl 代表已啟用輸出快取的使用者控制項。 當您存取 BasePartialCachingControl.CachePolicy 控制項的 PartialCachingControl 屬性時,一律會收到有效的 ControlCachePolicy 物件。 不過,如果您存取 UserControl.CachePolicy 控制項的 UserControl 屬性,只有當使用者控制項已經由 BasePartialCachingControl 控制項包裝時,才會收到有效的 ControlCachePolicy 物件。 如果未包裝它,當您嘗試操作它時, ControlCachePolicy 屬性所傳回的物件將會擲回例外狀況,因為它沒有相關聯的 BasePartialCachingControl 。 若要判斷實例是否 UserControl 支援快取而不產生例外狀況,請檢查 SupportsCaching 屬性。
使用 類別 ControlCachePolicy 是您可以啟用輸出快取的數種方式之一。 下列清單描述可用來啟用輸出快取的方法:
@ OutputCache
使用 指示詞在宣告式案例中啟用輸出快取。PartialCachingAttribute使用 屬性來啟用程式碼後置檔案中使用者控制項的快取。
使用 類別 ControlCachePolicy 在程式設計案例中指定快取設定,其中您使用 BasePartialCachingControl 先前其中一個方法啟用快取的實例,並使用 方法動態載入 TemplateControl.LoadControl 。 ControlCachePolicy實例只能在控制項生命週期的 和
PreRender
階段之間Init
成功操作。 如果您在階段之後修改 ControlCachePolicy 物件,ASP.NET 會擲回例外狀況,因為轉譯控制項之後PreRender
所做的任何變更都不會影響快取設定, (在階段) 期間Render
快取控制項。 最後,使用者控制項實例 (,因此其 ControlCachePolicy 物件) 僅適用于實際轉譯時的程式設計操作。
屬性
Cached |
取得或設定值,指出是否啟用使用者控制項的片段快取。 |
Dependency |
取得或設定 CacheDependency 類別的執行個體,而該執行個體是與已快取的使用者控制項輸出相關聯。 |
Duration |
取得或設定快取項目保留在輸出快取中的時間。 |
ProviderName |
取得或設定與控制項執行個體相關聯的輸出快取區提供者名稱。 |
SupportsCaching |
取得值,指出使用者控制項是否支援快取。 |
VaryByControl |
取得或設定控制識別項清單,用來變更快取輸出。 |
VaryByParams |
取得或設定 |
方法
Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
GetHashCode() |
做為預設雜湊函式。 (繼承來源 Object) |
GetType() |
取得目前執行個體的 Type。 (繼承來源 Object) |
MemberwiseClone() |
建立目前 Object 的淺層複製。 (繼承來源 Object) |
SetExpires(DateTime) |
指示包裝使用者控制項的 BasePartialCachingControl 控制項,讓快取項目在指定日期和時間到期。 |
SetSlidingExpiration(Boolean) |
指示包裝使用者控制項的 BasePartialCachingControl 控制項,將使用者控制項的快取項目設定成使用滑動期限或絕對期限。 |
SetVaryByCustom(String) |
設定輸出快取用來變更使用者控制項的自訂字串清單。 |
ToString() |
傳回代表目前物件的字串。 (繼承來源 Object) |
適用於
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應