Condividi tramite


ControlCachePolicy.Duration Proprietà

Definizione

Ottiene o imposta la quantità di tempo in cui gli elementi memorizzati nella cache devono rimanere nella cache di output.

public:
 property TimeSpan Duration { TimeSpan get(); void set(TimeSpan value); };
public TimeSpan Duration { get; set; }
member this.Duration : TimeSpan with get, set
Public Property Duration As TimeSpan

Valore della proprietà

Oggetto TimeSpan che rappresenta la quantità di tempo per cui un controllo utente deve rimanere nella cache di output. Il valore predefinito è Zero.

Eccezioni

Il controllo utente non è associato a e BasePartialCachingControl non è memorizzabile nella cache.

oppure

La Duration proprietà viene impostata all'esterno delle fasi di inizializzazione e rendering del controllo.

Esempio

Nell'esempio di codice seguente viene illustrato come un controllo utente può essere caricato in modo dinamico e modificato a livello di codice in fase di esecuzione. L'attributo PartialCachingAttribute viene applicato a un controllo utente denominato SimpleControl, il che significa che viene eseguito il wrapping da un PartialCachingControl controllo in fase di esecuzione. Le SimpleControl impostazioni di memorizzazione nella cache dell'oggetto possono essere modificate a livello di codice tramite l'oggetto associato ControlCachePolicy , disponibile tramite un riferimento al controllo che lo esegue a PartialCachingControl capo. In questo esempio, la proprietà viene esaminata durante l'inizializzazione Duration della pagina e la scadenza della cache viene modificata se vengono soddisfatte alcune condizioni. Questo esempio fa parte di un esempio più ampio fornito per 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>

Commenti

Se un criterio di scadenza assoluto viene impostato usando il SetSlidingExpiration metodo , la Duration proprietà restituisce la quantità di tempo rimanente fino alla scadenza della voce della cache.

Si applica a

Vedi anche