BasePartialCachingControl.CachePolicy Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Gets the ControlCachePolicy object that is associated with the wrapped user control.

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

public ControlCachePolicy CachePolicy { get; }

Property Value

Type: System.Web.UI.ControlCachePolicy

A ControlCachePolicy that stores output caching-related properties of the wrapped user control.

The CachePolicy property enables programmatic access to the ControlCachePolicy object associated with the user control contained by the BasePartialCachingControl instance. The ControlCachePolicy object can be programmatically manipulated to influence a user control's caching behavior and settings.

The following code example demonstrates how a user control can be loaded dynamically and manipulated programmatically at run time. This example has three parts:

  • A partial class, LogOnControl, that inherits from the UserControl base class and to which the PartialCachingAttribute attribute is applied.

  • A user control that is used with the LogOnControl partial class.

  • A Web Forms page that hosts the user control.

To run this example successfully, make sure the user control file (.ascx), its code-behind file (.cs or .vb), and the Web Forms page that hosts the user control (.aspx) are in the same directory.

The first part of the example demonstrates how the PartialCachingAttribute is applied to a user control named LogOnControl, which means the user control is wrapped by a PartialCachingControl control at run time. The LogOnControl object's caching settings can be programmatically manipulated through its associated ControlCachePolicy object, which is available through a reference to the PartialCachingControl that wraps it. In this example, caching settings are examined during page initialization and changed if some conditions are met.

using System;
using System.Web.UI;
using System.Web.UI.WebControls;

public class LogOnControl:UserControl
    public TextBox user;
    public TextBox password;

The second part of the example shows a user control that is used with the previous example to demonstrate user control caching.

<%@ control inherits = "LogOnControl" src = "LogOnControl.cs" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="">
<head id="Head1" runat="server">
    <title>ASP.NET Example</title>
<form id="Form1" runat="server">
<table style="font: 10pt verdana;border-width:1;border-style:solid;border-color:black;" cellspacing="15">
<td><b>Login: </b></td>
<td><asp:TextBox id="user" runat="server"/></td>
<td><b>Password: </b></td>
<td><asp:TextBox id="password" TextMode="Password" runat="server"/></td>

The third part of the example demonstrates using the LogOnControl user control from a Web Forms page.

<%@ Page Language="C#" Debug = "true"%>
<%@ Reference Control="Logonformcs.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 "Logonform.ascx" and "LogOnControl.cs" 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("Logonform.cs.ascx") as PartialCachingControl;        

    ControlCachePolicy cacheSettings = pcc.CachePolicy;

    // If the control is slated to expire in greater than 60 Seconds
    if (cacheSettings.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.        

.NET Framework
Available since 2.0
Return to top