ASP.net Substitution 页面缓存而部分不缓存的实现方法
程序员文章站
2024-03-09 18:01:35
文件1:deafault.aspx 复制代码 代码如下:<%@ page language="c#" autoeventwireup="true" codefile=...
文件1:deafault.aspx
<%@ page language="c#" autoeventwireup="true" codefile="default.aspx.cs" inherits="_default" %>
<%@ outputcache duration="10" varybyparam="none" %>
<!--设置缓存时间为10秒-->
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html>
<head id="head1" runat="server">
<title>substitution控件实例</title>
</head>
<body>
<form id="form1" runat="server">
<h1>substitution控件实例演示</h1>
您好<b>
<asp:substitution id="m1" methodname="getuser" runat="server">
</asp:substitution></b><br>
当前时间[非缓存]:
<b><asp:substitution id="m2" runat="server" methodname="gettime" /></b>
<br />
缓存: <%=datetime.now.tolongtimestring() %>
<asp:button id="button1" runat="server" text="刷新" />
</form>
</body>
</html>
文件2:deafault.aspx.cs
using system;
using system.data;
using system.configuration;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
public partial class _default : system.web.ui.page
{
//下面定义了两个表态方法,可以使用substitution控件调用,以实现这两个方法每次刷新时都要调用
public static string getuser(httpcontext context)
{
return context.user.identity.name;
}
public static string gettime(httpcontext context)
{
return datetime.now.tostring();
}
}
复制代码 代码如下:
<%@ page language="c#" autoeventwireup="true" codefile="default.aspx.cs" inherits="_default" %>
<%@ outputcache duration="10" varybyparam="none" %>
<!--设置缓存时间为10秒-->
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html>
<head id="head1" runat="server">
<title>substitution控件实例</title>
</head>
<body>
<form id="form1" runat="server">
<h1>substitution控件实例演示</h1>
您好<b>
<asp:substitution id="m1" methodname="getuser" runat="server">
</asp:substitution></b><br>
当前时间[非缓存]:
<b><asp:substitution id="m2" runat="server" methodname="gettime" /></b>
<br />
缓存: <%=datetime.now.tolongtimestring() %>
<asp:button id="button1" runat="server" text="刷新" />
</form>
</body>
</html>
文件2:deafault.aspx.cs
复制代码 代码如下:
using system;
using system.data;
using system.configuration;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
public partial class _default : system.web.ui.page
{
//下面定义了两个表态方法,可以使用substitution控件调用,以实现这两个方法每次刷新时都要调用
public static string getuser(httpcontext context)
{
return context.user.identity.name;
}
public static string gettime(httpcontext context)
{
return datetime.now.tostring();
}
}
上一篇: springMVC笔记