UserControl.CachePolicy 属性

定义

获取对该用户控件的缓存参数集合的引用。

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

属性值

一个 ControlCachePolicy,包含为该 UserControl 定义缓存参数的属性。

属性

示例

下面的代码示例演示如何使用 SupportsCaching 属性来确定用户控件是否可以缓存,如果是,则将 属性设置为 Duration 缓存控件 10 秒。

<%@ 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>

注解

属性 CachePolicy 定义 类实例的 UserControl 缓存参数。 属性 CachePolicy 是只读的;但是,可以设置它返回的对象 ControlCachePolicy 的属性。

CachePolicy无法以声明方式设置 属性。 必须以编程方式设置它。

适用于

产品 版本
.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

另请参阅