File 1: Deafault. aspx
Copy codeThe Code is as follows:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Default. aspx. cs" Inherits = "_ Default" %>
<% @ Outputcache duration = "10" varybyparam = "none" %>
<! -- Set the cache time to 10 seconds -->
<! 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 control instance </title>
</Head>
<Body>
<Form id = "Form1" runat = "server">
<H1> Substitution control instance demonstration Hello <B>
<Asp: substitution id = "m1" methodname = "GetUser" runat = "Server">
</Asp: substitution> </B> <br>
Current time [non-Cache]:
<B> <asp: Substitution ID = "m2" runat = "server" MethodName = "GetTime"/> </B>
<Br/>
Cache: <% = DateTime. Now. ToLongTimeString () %>
<Asp: Button ID = "Button1" runat = "server" Text = "refresh"/>
</Form>
</Body>
</Html>
File 2: Deafault. aspx. cs
Copy codeThe Code is as follows:
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
{
// The following two statement methods are defined. You can use the Substitution control to call these two methods each time they are refreshed.
Public static string GetUser (HttpContext context)
{
Return context. User. Identity. Name;
}
Public static String GetTime (HttpContext context)
{
Return DateTime. Now. ToString ();
}
}