In. net, Substitution caches local dynamics of the entire page (partial refresh is not cached)

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.