1<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ControlForControlCachePolicy.ascx.cs" Inherits="ControlForControlCachePolicy" %>
2<script language="C#" runat="server">
3    new void Page_Load(object sender, EventArgs e)
4    {
5        Label1.Text ="用户控件的当前时间为---<font color='red'>"+ DateTime.Now.ToString()+"</font>";
6    }

7</script>
8<asp:Label ID="Label1" runat="server" Height="24px" Width="560px"></asp:Label>
Response.Cache的数据缓存:

 1%@ OutputCache Duration="5" VaryByParam="none" %>
 2<script language="C#" runat="server">
 3
 4    protected void Page_Load(object sender, EventArgs e)
 5    {
 6        //设置缓存为当前时间加5秒
 7        Response.Cache.SetExpires(DateTime.Now.AddSeconds(5));
 8        //在API设置的时候必须指定这一句,设置缓存存放位置
 9        Response.Cache.SetCacheability(HttpCacheability.Public);
10        //将缓存设置从绝对时间设置为过期时间
11        Response.Cache.SetSlidingExpiration(false);
12        //忽略表头
13        Response.Cache.SetValidUntilExpires(true);
14        Response.Write(DateTime.Now);
15    }

Substitution缓存的设置:

 1<%@ OutputCache Duration="5" VaryByParam="None" %>
 2<script language="C#" runat="server">
 3   new void Page_Load(object sender, EventArgs e)
 4    {
 5        Response.Write("页面加载内容,设置为5秒缓存过期:-------"
 6            +"<font color='red'>"+DateTime.Now.ToString()+"</font><br>");
 7        Response.Write(Server.HtmlEncode("Server.HtmlEncode<hr color='red'>")+"<br>");
 8        Response.Write(Server.HtmlDecode("Server.HtmlDecode<br><hr color='red' size='3pt'>"));
 9    }

10    public static string SubstitutionFun(HttpContext context)
11    {
12        return ("Substitution控件中的时间显示效果(没有缓存):-------"
13            +"<font color='green'>"+DateTime.Now.ToString()+"</Font>");
14    }

15    public static string SubstitutionFunWrite(HttpContext context)
16    {
17        return ("Substitution控件中的时间显示效果(没有缓存):-------"
18            + "<font color='blue'>" + DateTime.Now.ToString() + "</Font>");
19    }

20</script>
21<html xmlns="http://www.w3.org/1999/xhtml" >
22<head runat="server">
23    <title>无标题页</title>
24</head>
25<body>
26    <form id="form1" runat="server">
27    <div>
28        <asp:Substitution ID="Substitution1" MethodName="SubstitutionFun" runat="server" />
29        <br />
30        <br />
31        <% Response.WriteSubstitution(new HttpResponseSubstitutionCallback(SubstitutionFunWrite)); %>
32    </div>
33    </form>
34</body>
35</html>


页面加用户控件的数据缓存:

用户控件代码:

1<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ControlForControlCachePolicy.ascx.cs" Inherits="ControlForControlCachePolicy" %>
2<script language="C#" runat="server">
3    new void Page_Load(object sender, EventArgs e)
4    {
5        Label1.Text ="用户控件的当前时间为---<font color='red'>"+ DateTime.Now.ToString()+"</font>";
6    }

7</script>
8<asp:Label ID="Label1" runat="server" Height="24px" Width="560px"></asp:Label>

页面调用代码:

 1<%@ Register Src="ControlForControlCachePolicy.ascx" TagName="ControlForControlCachePolicy"
 2    TagPrefix="uc1" %>
 3    <%@ OutputCache Duration="5" VaryByParam="None" %>
 4
 5<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 6<script language="C#" runat="server">
 7    void Page_Init(object sender, EventArgs e)
 8    {
 9        //动态加载用户控件,并返回PartialCachingControl实例
10        PartialCachingControl ppc = LoadControl("ControlForControlCachePolicy.ascx"as PartialCachingControl;
11        //通过CachePolicy属性获取ControlCachePolicy实例
12        ControlCachePolicy cacheSettings = ppc.CachePolicy;
13        //如果用户控件的过期时间大于20秒就
14        if (cacheSettings.Duration > TimeSpan.FromSeconds(20))
15        
16         //设置用户控件过期时间和缓存过期测略
17            cacheSettings.SetExpires(DateTime.Now.Add(TimeSpan.FromSeconds(10)));
18            cacheSettings.SetSlidingExpiration(false);
19        }

20        Controls.Add(ppc);
21    }

22    new void Page_Load(object sender, EventArgs e)
23    {
24        Label1.Text = "本页面的控件显示的时间为:(本页面缓存设置为5秒):<font color='blue'>" 
25            + DateTime.Now.ToString()+"</font>";
26    }

27</script>
28<html xmlns="http://www.w3.org/1999/xhtml" >
29<head runat="server">
30    <title>无标题页</title>
31</head>
32<body>
33    <form id="form1" runat="server">
34    <div>
35        <br />
36        &nbsp;<br />
37        <br />
38        <br />
39        <asp:Label ID="Label1" runat="server" Text="Label" Width="560px" Height="24px"></asp:Label>&nbsp;</div>
40    </form>
41</body>
42</html>
43

<%@ Reference Control="~/ControlForControlCachePolicy.ascx" %> 这一句加入页面的头部 用于将用户控件ControlForControlCachePolicy.ascx 控件的动态编辑和连接
posted on 2006-12-25 23:27  小角色  阅读(229)  评论(0)    收藏  举报