使用部分页面缓存
1、使用缓存后替换
AdRotator控件的内部使用了缓存后替换。
使用Substitution控件
SubstitutionControl.aspx
以编程方式使用Response.WriteSubstitution();
ShowWriteSubstitution.aspx
2011-5-28 22:26 danny
2、用户控件缓存
缓存Asp.net页面一样在内存中缓存用户控件呈现的内容。
不给用户控件添加<%@OutputCache%>指令时,用户控件的输出内容就会缓存。
注意:
当缓存一个用户控件时,内容缓存在Web服务器上,而不是缓存在代理服务器和浏览器上。当代理服务器或浏览器缓存页面时,它们总是缓存整个页面。
Movies.ascx
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
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