Export (0) Print
Expand All

BasePartialCachingControl.CachePolicy Property

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;

[PartialCaching(100)]
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"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
<form id="Form1" runat="server">
<table style="font: 10pt verdana;border-width:1;border-style:solid;border-color:black;" cellspacing="15">
<tr>
<td><b>Login: </b></td>
<td><asp:TextBox id="user" runat="server"/></td>
</tr>
<tr>
<td><b>Password: </b></td>
<td><asp:TextBox id="password" TextMode="Password" runat="server"/></td>
</tr>
<tr>
</tr>
</table>
</form>
</body>
</html>

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.        
        cacheSettings.SetExpires(DateTime.Now.Add(TimeSpan.FromSeconds(30)));
        cacheSettings.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