See Also: ControlCachePolicy Members
The System.Web.UI.ControlCachePolicy class is used by developers in programmatic user control scenarios to specify output caching settings for user controls (.ascx files). ASP.NET embeds user controls within a System.Web.UI.BasePartialCachingControl instance. The System.Web.UI.BasePartialCachingControl class represents a user control that has output caching enabled. When you access the BasePartialCachingControl.CachePolicy property of a System.Web.UI.PartialCachingControl control, you will always receive a valid System.Web.UI.ControlCachePolicy object. However, if you access the UserControl.CachePolicy property of a System.Web.UI.UserControl control, you receive a valid System.Web.UI.ControlCachePolicy object only if the user control is already wrapped by a System.Web.UI.BasePartialCachingControl control. If it is not wrapped, the System.Web.UI.ControlCachePolicy object returned by the property will throw exceptions when you attempt to manipulate it because it does not have an associated System.Web.UI.BasePartialCachingControl. To determine whether a System.Web.UI.UserControl instance supports caching without generating exceptions, inspect the ControlCachePolicy.SupportsCaching property.
Using the System.Web.UI.ControlCachePolicy class is one of several ways you can enable output caching. The following list describes methods you can use to enable output caching:
Use the @ OutputCache directive to enable output caching in declarative scenarios.
Use the System.Web.UI.PartialCachingAttribute attribute to enable caching for a user control in a code-behind file.
Use the System.Web.UI.ControlCachePolicy class to specify cache settings in programmatic scenarios in which you are working with System.Web.UI.BasePartialCachingControl instances that have been cache-enabled using one of the previous methods and dynamically loaded using the erload:System.Web.UI.TemplateControl.LoadControl method. A System.Web.UI.ControlCachePolicy instance can be successfully manipulated only between the Init and PreRender stages of the control life cycle. If you modify a System.Web.UI.ControlCachePolicy object after the PreRender phase, ASP.NET throws an exception, because any changes made after the control is rendered cannot actually affect cache settings (a control is cached during the Render stage). Finally, a user control instance (and therefore its System.Web.UI.ControlCachePolicy object) is only available for programmatic manipulation when it is actually rendered.