文件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();
}
}