檔案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();
}
}