Export (0) Print
Expand All

ControlCachePolicy.Duration Property

Gets or sets the amount of time that cached items are to remain in the output cache.

Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)

public TimeSpan Duration { get; set; }

Property Value

Type: System.TimeSpan
A TimeSpan that represents the amount of time a user control is to remain in the output cache. The default is Zero.

ExceptionCondition
HttpException

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

- or -

The Duration property is set outside of the initialization and rendering stages of the control.

If an absolute expiration policy is set using the SetSlidingExpiration method, the Duration property returns the amount of time remaining until cache entry expiration.

The following code example demonstrates how a user control can be loaded dynamically and manipulated programmatically at run time. The PartialCachingAttribute attribute is applied to a user control named SimpleControl, 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, the Duration property is examined during page initialization and cache expiration is 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>

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft