Secrets of Reading Notes in ASP. net2.0-page output CacheArticleWe talked about how to cache all the page output. When we only need to cache part of the page content, while the other part of the content needs to be dynamically updated, at this time, we can use some page caching technologies.
There are two ways to enable partial page cache:
(1) Post-cache replacement technology (post-Cache substitution)
(2) Use user controls to cache a specific area of a page, rather than the whole page.
The post-cache replacement technology can be used in declarative or programmatic ways (this name cannot be obtained ). If you want to use the cache with replacement as a declaration, you must use the ASP. NET substitution control.
Code
<% @ Page Language = " C # " %>
<% @ Outputcache duration = " 15 " Varybyparam = " None " %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< Script Runat = "Server" >
Public static string gettime (httpcontext context)
{
Return Datetime. Now. tostring ( " T " );
}
</ Script >
< Html Xmlns = "Http://www.w3.org/1999/xhtml" >
< Head ID = "Head1" Runat = "Server" >
< Title > Substitution Control </ Title >
</ Head >
< Body >
< Form ID = "Form1" Runat = "Server" >
< Div >
The cached time is: <% = Datetime. Now. tostring ( " T " ) %>
< HR />
The substitution time is:
< ASP: Substitution
ID = "Substitution1"
Methodname = "Gettime"
Runat = "Server" />
</ Div >
</ Form >
</ Body >
</ Html >
The substitution control has an important attribute: methodname. The methodname attribute accepts the name of a method defined on the page. The method must be static because the page instance has not been created when the page output is cached.
The content displayed by user controls can be cached in the memory like ASP. NET pages. When the command % @ outputcache % is added to the user control, the output content of the user control is cached.
Code
<% @ Control Language = " C # " Classname = " Movies " %>
<% @ Outputcache duration = " 600 " Varybyparam = " None " %>
User Control time:
<%=Datetime. Now. tostring ("T")%>
< ASP: gridview
ID = "Grdmovies"
Performanceid = "Srcmovies"
Runat = "Server" />
< ASP: sqldatasource
ID = "Srcmovies"
Connectionstring = "<% $ Connectionstrings: Movies %>"
Selectcommand = "Select title, Director from movies"
Runat = "Server" />