UserControl.CachePolicy Property

Definition

Gets a reference to a collection of caching parameters for this user control.

[System.ComponentModel.Browsable(false)]
public System.Web.UI.ControlCachePolicy CachePolicy { get; }

Property Value

A ControlCachePolicy containing properties that define the caching parameters for this UserControl.

Attributes

Examples

The following code example demonstrates how to use the SupportsCaching property to determine whether the user control can be cached, and if so, sets the Duration property to cache the control for 10 seconds.

<%@ page language="C#"%>
<%@ register src="SimpleControlcs.ascx" 
             tagname="SimpleControl" 
             tagprefix="uc1"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void Page_Init(object sender, EventArgs e)
{
  // If the control is already in the cache, calling properties
  // will throw an exception. Make sure the control is available. 
  if (SimpleControl1 != null)
  {
    
    Page.DataBind();
    
    if (SimpleControl1.CachePolicy.SupportsCaching)
    {
      // Set the cache duration to 10 seconds.
      SimpleControl1.CachePolicy.Duration = new TimeSpan(0, 0, 10);
      
    }
  }
}

</script>

Remarks

The CachePolicy property defines the cache parameters for the instance of the UserControl class. The CachePolicy property is read-only; however, you can set the properties of the ControlCachePolicy object that it returns.

The CachePolicy property cannot be set declaratively. It must be set programmatically.

Applies to

Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

See also