Substitution control-dynamically update the cache page

Source: Internet
Author: User

When an ASP. NET page is cached, all output of the page is cached by default. In the first request, the page runs and caches its output. Subsequent requests will be completed through caching, and the code on this page will not run.

In some cases, the ASP. NET page may be cached, but you need to update the selected part of the page according to each request. For example, you may want to cache a large part of a page, but you need to dynamically update the time-related information on the page.

You can use the Substitution control to insert dynamic content into the cache page. The Substitution control does not render any tags. You need to bind the control to a method on the page or the parent user control. You must create a static method to return any information to be inserted into the page. The method called by the Substitution control must comply with the following standards:

  • This method is defined as a static method (shared method in Visual Basic ).

  • This method is accepted Type parameter.

  • This method returns Type value.

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.

The following code example demonstrates how to use the Substitution control to create dynamically updated content on the cache page. Page The code in the event is updated with the current time Control. Because the page cache duration is set to 60 seconds, the text of the Label control is not changed, even if the page is requested multiple times within 60 seconds. The Substitution control on the page calls the static methodGetTimeThis method returns the current time as a string. The value of the Substitution control is updated each time the page is refreshed.

The following two methods do not demonstrate this feature:

Code 1:

<Script runat = "server">
Static string getCurrentTime (HttpContext context)
{
Return DateTime. Now. ToString ();
}
Void Page_Load (object sender, EventArgs e)
{
Response. Cache. SetExpires (DateTime. Now. AddSeconds (600 ));
Response. Cache. SetCacheability (HttpCacheability. Public );
Response. Cache. SetValidUntilExpires (true );

}
</Script>

<Div>
<H4>
Insert dynamic content on the cache page-use the substitution Control for demonstration <P>
Cache Time: <% = DateTime. Now. ToString () %>
</P>
<P>
<B> Real Time: <asp: Substitution ID = "Substitution1" runat = "server" MethodName = "getCurrentTime"/>
</B>
</P>
</Div>

Code 2:

<% @ OutputCache Duration = "600" VaryByParam = "none" %>

<Script runat = "server">
Static string getCurrentTime (HttpContext context)
{
Return DateTime. Now. ToString ();
}

</Script>

<Div>
<H4>
Insert dynamic content on the cache page-use the substitution Control for demonstration <P>
Cache Time: <% = DateTime. Now. ToString () %>
</P>
<P>
<B> Real Time: <asp: Substitution ID = "Substitution1" runat = "server" MethodName = "getCurrentTime"/>
</B>
</P>
</Div>

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.