Edit

Share via


ControlCachePolicy.SetSlidingExpiration(Boolean) Method

Definition

Instructs the BasePartialCachingControl control that wraps the user control to set the user control's cache entry to use sliding or absolute expiration.

public:
 void SetSlidingExpiration(bool useSlidingExpiration);
public void SetSlidingExpiration (bool useSlidingExpiration);
member this.SetSlidingExpiration : bool -> unit
Public Sub SetSlidingExpiration (useSlidingExpiration As Boolean)

Parameters

useSlidingExpiration
Boolean

true to use sliding cache expiration instead of absolute expiration; otherwise, false.

Exceptions

The user control is not associated with a BasePartialCachingControl and is not cacheable.

Examples

The following code example demonstrates how a user control can be loaded dynamically and manipulated programmatically at run time. A user control named SimpleControl is decorated with a PartialCachingAttribute attribute, which means it is wrapped by a PartialCachingControl control at run time. The SimpleControl object's caching settings can be programmatically manipulated through its associated ControlCachePolicy object, which is available through a reference to the PartialCachingControl control that wraps it. In this example, caching settings are examined during page initialization and changed if some conditions are met. This example is part of a larger example provided for the ControlCachePolicy class.

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

Remarks

Use the SetExpires and SetSlidingExpiration methods (passing true) to instruct the BasePartialCachingControl control that wraps the user control to use a sliding expiration caching policy instead of an absolute expiration policy. Use the SetExpires method and the SetSlidingExpiration method (passing false) to specify an absolute expiration policy.

Applies to

See also