In. net, Substitution control is a very good control. It can cache the entire page, but it can also make local Dynamic Refresh, a bit ajax flavor. The following small series will introduce it to you.
This method is defined as a static method (shared method in Visual Basic ).
This method accepts parameters of the HttpContext type.
This method returns a value of the String type.
Note: The Substitution control cannot access other controls on the page, that is, you cannot check or change the values of other controls. However, the Code can indeed use the parameters passed to it to access the context of the current page.
When the page is running, the Substitution Control calls this method and replaces the Substitution control on the page with the return value of this method.
Example:
The Code is as follows: |
Copy code |
Response. Cache. SetExpires (DateTime. Now. AddSeconds (5 )); Response. Cache. SetCacheability (HttpCacheability. Public ); Response. Cache. SetValidUntilExpires (true ); Static string getTime (HttpContext context) { Return DateTime. Now. ToString (); } <% = DateTime. Now. ToString () %> <br/> <Asp: Substitution ID = "Substitution1" runat = "server" MethodName = "getTime"/> Or <% @ OutputCache Duration = "10" VaryByParam = "none" %> <% = DateTime. Now. ToString () %> <br/> <Asp: Substitution ID = "Substitution1" runat = "server" MethodName = "getTime"/> |
My own application instance
Index. aspx File
The Code is as follows: |
Copy code |
<% @ 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> |
Index. aspx. cs File
The Code is as follows: |
Copy code |
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 (); } }
|