共用方式為


BasePartialCachingControl.CachePolicy 屬性

定義

取得與已包裝好的使用者控制項相關聯的 ControlCachePolicy 物件。

public:
 property System::Web::UI::ControlCachePolicy ^ CachePolicy { System::Web::UI::ControlCachePolicy ^ get(); };
public System.Web.UI.ControlCachePolicy CachePolicy { get; }
member this.CachePolicy : System.Web.UI.ControlCachePolicy
Public ReadOnly Property CachePolicy As ControlCachePolicy

屬性值

ControlCachePolicy

ControlCachePolicy,可儲存已包裝好的使用者控制項之輸出快取相關屬性。

範例

下列程式碼範例示範如何在執行時間以程式設計方式動態載入使用者控制項。 此範例有三個部分:

  • 部分類別 LogOnControl ,繼承自 UserControl 基類,以及 PartialCachingAttribute 套用屬性的類別。

  • 與部分類別搭配 LogOnControl 使用的使用者控制項。

  • 裝載使用者控制項的Web Form頁面。

若要成功執行此範例,請確定使用者控制項檔案 (.ascx) 、其程式碼後置檔案 (.cs 或 .vb) ,以及裝載使用者控制項 (.aspx) 的 Web Form 頁面位於相同的目錄中。

範例的第一個部分示範 如何將 PartialCachingAttribute 套用至名為 LogOnControl 的使用者控制項,這表示使用者控制項會在執行時間由 PartialCachingControl 控制項包裝。 物件的 LogOnControl 快取設定可以透過其相關聯的 ControlCachePolicy 物件以程式設計方式操作,而該物件可透過包裝它的 參考 PartialCachingControl 取得。 在此範例中,快取設定會在頁面初始化期間進行檢查,並在符合某些條件時變更。

using System;
using System.Web.UI;
using System.Web.UI.WebControls;

[PartialCaching(100)]
public class LogOnControl:UserControl
{
    public TextBox user;
    public TextBox password;
}
Imports System.Web.UI
Imports System.Web.UI.WebControls

<PartialCaching(100)> _
Public Class LogOnControl
   Inherits UserControl

   Public user As TextBox
   Public password As TextBox

End Class

範例的第二個部分會顯示與上一個範例搭配使用的使用者控制項,以示範使用者控制項快取。

<%@ control inherits = "LogOnControl" src = "LogOnControl.cs" %>
<!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 style="font: 10pt verdana;border-width:1;border-style:solid;border-color:black;" cellspacing="15">
<tr>
<td><b>Login: </b></td>
<td><asp:TextBox id="user" runat="server"/></td>
</tr>
<tr>
<td><b>Password: </b></td>
<td><asp:TextBox id="password" TextMode="Password" runat="server"/></td>
</tr>
<tr>
</tr>
</table>
</form>
</body>
</html>
<%@ control inherits = "LogOnControl" src = "LogOnControl.vb" %>
<!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 style="font: 10pt verdana;border-width:1;border-style:solid;border-color:black;" cellspacing="15">
<tr>
<td><b>Login: </b></td>
<td><ASP:TextBox id="user" runat="server"/></td>
</tr>
<tr>
<td><b>Password: </b></td>
<td><ASP:TextBox id="password" TextMode="Password" runat="server"/></td>
</tr>
<tr>
</tr>
</table>
</form>
</body>
</html>

此範例的第三個部分示範如何使用 LogOnControl 來自Web Form頁面的使用者控制項。

<%@ Page Language="C#" Debug = "true"%>
<%@ Reference Control="Logonformcs.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 "Logonform.ascx" and "LogOnControl.cs" 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("Logonform.cs.ascx") as PartialCachingControl;        
    
    ControlCachePolicy cacheSettings = pcc.CachePolicy;
    
    // If the control is slated to expire in greater than 60 Seconds
    if (cacheSettings.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.        
        cacheSettings.SetExpires(DateTime.Now.Add(TimeSpan.FromSeconds(30)));
        cacheSettings.SetSlidingExpiration(false);
    }                    
    Controls.Add(pcc);
}
</script>
<%@ Page Language="VB" Debug = "true"%>
<%@ Reference Control="Logonformvb.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 "Logonform.ascx" and "LogOnControl.vb" file in 
    ' the same directory as the aspx file.
    Sub Page_Init(ByVal Sender As Object, ByVal e As EventArgs)

        ' Obtain a PartialCachingControl object which wraps the 'LogOnControl' user control.
        Dim pcc As PartialCachingControl
        pcc = CType(LoadControl("Logonform.vb.ascx"), PartialCachingControl)
    
        Dim cacheSettings As ControlCachePolicy
        cacheSettings = pcc.CachePolicy
    
        ' If the control is slated to expire in greater than 60 Seconds
        If (cacheSettings.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.        
            cacheSettings.SetExpires(DateTime.Now.Add(TimeSpan.FromSeconds(30)))
            cacheSettings.SetSlidingExpiration(False)
        End If
        Controls.Add(pcc)

    End Sub ' Page_Init
              
</script>

備註

CachePolicy屬性可讓您以程式設計方式存取 ControlCachePolicy 實例所包含之使用者控制項相關聯的 BasePartialCachingControl 物件。 ControlCachePolicy物件可以透過程式設計方式操作,以影響使用者控制項的快取行為和設定。

適用於

另請參閱