ControlCachePolicy クラス
アセンブリ: System.Web (system.web.dll 内)
構文
解説
ControlCachePolicy クラスは、ユーザー コントロールのプログラムを作成するときに、ユーザー コントロール (.ascx ファイル) の出力キャッシュ設定を指定するために開発者が使用します。ASP.NET では、ユーザー コントロールは BasePartialCachingControl インスタンス内に埋め込まれます。BasePartialCachingControl クラスは、出力キャッシュが有効になったユーザー コントロールを表すクラスです。PartialCachingControl コントロールの BasePartialCachingControl.CachePolicy プロパティにアクセスすると、有効な ControlCachePolicy オブジェクトをいつでも受け取ることができます。ただし、UserControl コントロールの UserControl.CachePolicy プロパティにアクセスする場合は、ユーザー コントロールが既に BasePartialCachingControl コントロールによってラップされている場合しか、有効な ControlCachePolicy オブジェクトを受け取ることはできません。ユーザー コントロールがラップされていない場合は、プロパティから返された ControlCachePolicy オブジェクトを操作しようとすると例外がスローされます。これは、このオブジェクトに関連付けられた BasePartialCachingControl が存在しないためです。UserControl インスタンスでキャッシュがサポートされているかどうかを、例外を生成せずに判断するには、SupportsCaching プロパティを調べます。
出力キャッシュを有効にできる方法の 1 つは、ControlCachePolicy クラスを使用することです。出力キャッシュを有効にするために使用できる方法は、次のとおりです。
-
分離コード ファイルのユーザー コントロールのキャッシュを有効にするには、PartialCachingAttribute 属性を使用します。
-
プログラム内で、既に説明したいずれかのメソッドでキャッシュを有効にし、System.Web.UI.TemplateControl.LoadControl メソッドによって動的に読み込んだ BasePartialCachingControl インスタンスを使用している場合、そのプログラムからキャッシュ設定を指定するには、ControlCachePolicy クラスを使用します。ControlCachePolicy インスタンスを正常に操作できるのは、コントロールの有効期間のうち Init 段階から PreRender 段階までの間のみです。PreRender 段階よりも後で ControlCachePolicy オブジェクトに変更を加えると、ASP.NET から例外がスローされます。コントロールが表示された後で加えられた変更は、実際にはキャッシュの設定に反映されないためです (コントロールは Render 段階でキャッシュされます)。最後に、ユーザー コントロールのインスタンス (つまり ControlCachePolicy オブジェクト) が実際に表示された後は、プログラムでしかそのインスタンスを操作できません。
実行時にプログラムからユーザー コントロールを動的に読み込み、操作する方法を次のコード例に示します。PartialCachingAttribute 属性が SimpleControl というユーザー コントロールに適用されます。これは、ユーザー コントロールが実行時に PartialCachingControl コントロールによってラップされることを意味します。SimpleControl オブジェクトのキャッシュ設定は、関連付けられた ControlCachePolicy オブジェクト (このオブジェクトをラップする PartialCachingControl コントロールへの参照を使用してアクセス可能) を通じてプログラムから操作できます。この例では、ページの初期化中に Duration プロパティを調べ、いくつかの条件を満たした場合、SetSlidingExpiration メソッドと SetExpires メソッドを使用して変更を加えます。
<%@ 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>
<%@ 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>
Web フォーム ページから SimpleControl ユーザー コントロールを使用する方法を次のコード例に示します。この例を正常に実行するには、ユーザー コントロール ファイル (.ascx)、それに対応する分離コード ファイル (.cs または .vb)、およびそのユーザー コントロール (.aspx) をホストする Web フォーム ページが同じディレクトリ内にあることを確認してください。
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
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; } }
<%@ Control Language="VB" AutoEventWireup="true" CodeFile="SimpleControl.ascx.vb" Inherits="SimpleControl" %> <form> <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>
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="SimpleControl.ascx.cs" Inherits="SimpleControl" %> <form> <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>
System.Web.UI.ControlCachePolicy
プラットフォーム
Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
Weblioに収録されているすべての辞書からControlCachePolicy クラスを検索する場合は、下記のリンクをクリックしてください。
全ての辞書からControlCachePolicy クラス を検索
- ControlCachePolicy クラスのページへのリンク