14、使用部分页面缓存

使用部分页面缓存

1、使用缓存后替换
AdRotator控件的内部使用了缓存后替换。
使用Substitution控件

SubstitutionControl.aspx

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
public static string GetTime(HttpContext context)
{
return DateTime.Now.ToString("T");
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
The cache time is :<%=DateTime.Now.ToString("T") %>
<hr />
The substitution time is:
<asp:Substitution ID="Substitution1" MethodName="GetTime" runat="server" />
</div>
</form>
</body>
</html>


以编程方式使用Response.WriteSubstitution();

ShowWriteSubstitution.aspx

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
public static string GetTime(HttpContext context)
{
return DateTime.Now.ToString("T");
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
The cache time is :<%=DateTime.Now.ToString("T") %>
<hr />
The substitution time is:
<% Response.WriteSubstitution(GetTime); %>
</div>
</form>
</body>
</html>


2011-5-28 22:26 danny

2、用户控件缓存
缓存Asp.net页面一样在内存中缓存用户控件呈现的内容。
不给用户控件添加<%@OutputCache%>指令时,用户控件的输出内容就会缓存。

注意:
当缓存一个用户控件时,内容缓存在Web服务器上,而不是缓存在代理服务器和浏览器上。当代理服务器或浏览器缓存页面时,它们总是缓存整个页面。

Movies.ascx

<%@ Control Language="C#" ClassName="Movies" %>
<%@ OutputCache Duration="600" VaryByParam="none" %>
<script runat="server">

</script>

User Control Time:

<%=DateTime.Now.ToString("T") %>

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False"
ReadOnly="True" SortExpression="id" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Director" HeaderText="Director"
SortExpression="Director" />
<asp:BoundField DataField="DateReleased" HeaderText="DateReleased"
SortExpression="DateReleased" />
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [Movies]"></asp:SqlDataSource>


ShowUserControlCache.aspx
[code]
<%@ Page Language="C#" %>

<%@ Register TagPrefix="user" TagName="Movies" Src="~/Movies.ascx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
Page Time:
<%=DateTime.Now.ToString("T") %>
<hr />
<user:Movies ID="Movies" runat="server" />
</div>
</form>
</body>
</html>
[/code]

2011-5-29 19:10 danny
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值