Freigeben über


BasePartialCachingControl.CachePolicy Eigenschaft

Definition

Ruft das ControlCachePolicy Objekt ab, das dem umschlossenen Benutzersteuerelement zugeordnet ist.

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

Eigenschaftswert

A ControlCachePolicy that stores output caching-related properties of the wrapped user control.

Beispiele

Im folgenden Codebeispiel wird veranschaulicht, wie ein Benutzersteuerelement dynamisch geladen und programmgesteuert zur Laufzeit bearbeitet werden kann. In diesem Beispiel sind drei Teile enthalten:

  • Eine partielle Klasse, LogOnControldie von der UserControl Basisklasse erbt und auf die das PartialCachingAttribute Attribut angewendet wird.

  • Ein Benutzersteuerelement, das mit der LogOnControl partiellen Klasse verwendet wird.

  • Eine Webseite, auf der das Benutzersteuerelement gehostet wird.

Um dieses Beispiel erfolgreich auszuführen, stellen Sie sicher, dass sich die Benutzersteuerelementdatei (ASCX), die CodeBehind-Datei (.cs oder .vb) und die Web Forms-Seite, auf der das Benutzersteuerelement (.aspx) gehostet wird, im selben Verzeichnis befinden.

Im ersten Teil des Beispiels wird veranschaulicht, wie das PartialCachingAttribute Steuerelement auf ein Benutzersteuerelement mit dem Namen LogOnControlangewendet wird, was bedeutet, dass das Benutzersteuerelement zur Laufzeit von einem PartialCachingControl Steuerelement umschlossen wird. Die LogOnControl Zwischenspeicherungseinstellungen des Objekts können programmgesteuert über das zugeordnete ControlCachePolicy Objekt bearbeitet werden, das über einen Verweis auf den PartialCachingControl Umbruch verfügbar ist. In diesem Beispiel werden Zwischenspeicherungseinstellungen während der Seiteninitialisierung untersucht und geändert, wenn einige Bedingungen erfüllt sind.

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

Der zweite Teil des Beispiels zeigt ein Benutzersteuerelement, das mit dem vorherigen Beispiel verwendet wird, um das Zwischenspeichern von Benutzersteuerelementen zu veranschaulichen.

<%@ 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>

Der dritte Teil des Beispiels veranschaulicht die Verwendung des LogOnControl Benutzersteuerelements von einer Web Forms-Seite.

<%@ 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>

Hinweise

Die CachePolicy Eigenschaft ermöglicht den programmgesteuerten Zugriff auf das Objekt, das ControlCachePolicy dem in der BasePartialCachingControl Instanz enthaltenen Benutzersteuerelement zugeordnet ist. Das ControlCachePolicy Objekt kann programmgesteuert bearbeitet werden, um das Zwischenspeicherungsverhalten und die Einstellungen eines Benutzersteuerelements zu beeinflussen.

Gilt für:

Weitere Informationen