BasePartialCachingControl.CachePolicy Property
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets the ControlCachePolicy object that is associated with the wrapped user control.
public:
property System::Web::UI::ControlCachePolicy ^ CachePolicy { System::Web::UI::ControlCachePolicy ^ get(); };
public System.Web.UI.ControlCachePolicy CachePolicy { get; }
member this.CachePolicy : System.Web.UI.ControlCachePolicy
Public ReadOnly Property CachePolicy As ControlCachePolicy
Property Value
A ControlCachePolicy that stores output caching-related properties of the wrapped user control.
Examples
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;
}
Imports System.Web.UI
Imports System.Web.UI.WebControls
<PartialCaching(100)> _
Public Class LogOnControl
Inherits UserControl
Public user As TextBox
Public password As TextBox
End Class
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>
<%@ control inherits = "LogOnControl" src = "LogOnControl.vb" %>
<!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>
<%@ Page Language="VB" Debug = "true"%>
<%@ Reference Control="Logonformvb.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 "Logonform.ascx" and "LogOnControl.vb" file in
' the same directory as the aspx file.
Sub Page_Init(ByVal Sender As Object, ByVal e As EventArgs)
' Obtain a PartialCachingControl object which wraps the 'LogOnControl' user control.
Dim pcc As PartialCachingControl
pcc = CType(LoadControl("Logonform.vb.ascx"), PartialCachingControl)
Dim cacheSettings As ControlCachePolicy
cacheSettings = pcc.CachePolicy
' If the control is slated to expire in greater than 60 Seconds
If (cacheSettings.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.
cacheSettings.SetExpires(DateTime.Now.Add(TimeSpan.FromSeconds(30)))
cacheSettings.SetSlidingExpiration(False)
End If
Controls.Add(pcc)
End Sub ' Page_Init
</script>
Remarks
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.
Applies to
See also
.NET