Partager via


ControlCachePolicy.SetExpires(DateTime) Méthode

Définition

Indique au BasePartialCachingControl contrôle qui encapsule le contrôle utilisateur d’expirer l’entrée du cache à la date et à l’heure spécifiées.

public:
 void SetExpires(DateTime expirationTime);
public void SetExpires(DateTime expirationTime);
member this.SetExpires : DateTime -> unit
Public Sub SetExpires (expirationTime As DateTime)

Paramètres

expirationTime
DateTime

Après DateTime quoi l’entrée mise en cache expire.

Exceptions

Le contrôle utilisateur n’est pas associé à un BasePartialCachingControl contrôle et n’est pas mis en cache.

Exemples

L’exemple de code suivant montre comment un contrôle utilisateur peut être chargé dynamiquement et manipulé par programmation au moment de l’exécution. L’attribut PartialCachingAttribute est appliqué à un contrôle utilisateur nommé SimpleControl, ce qui signifie que le contrôle utilisateur est encapsulé par un PartialCachingControl contrôle au moment de l’exécution. Les SimpleControl paramètres de mise en cache de l’objet peuvent être manipulés par programmation via son objet associé ControlCachePolicy , qui est disponible via une référence au PartialCachingControl contrôle qui l’encapsule. Dans cet exemple, la propriété est examinée pendant l’initialisation Duration de la page et modifiée à l’aide des SetSlidingExpiration méthodes et SetExpires des méthodes si certaines conditions sont remplies. Cet exemple fait partie d’un exemple plus large fourni pour la ControlCachePolicy classe.

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

Remarques

Utilisez les SetExpires méthodes ( SetSlidingExpiration passage true) pour indiquer au BasePartialCachingControl contrôle qui encapsule le contrôle utilisateur pour utiliser une stratégie de mise en cache d’expiration glissante au lieu d’une stratégie d’expiration absolue. Utilisez la méthode et la SetExpiresSetSlidingExpiration méthode (passage false) pour spécifier une stratégie d’expiration absolue.

S’applique à

Voir aussi