ASP.net Substitution 页面缓存而部分不缓存的实现方法
时间:2021-07-01 10:21:17
帮助过:25人阅读
文件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();
}
}