Partial-Page Output Caching
1. Create a new website
2. Switch to design mode on the default aspx page.
3. Insert a table with 4 rows, 2 columns.
4. Type “Time Now:” in first column of row 1.
5. Add a Label control to the second column of row1.
6. Create a custom label control
1. Create a folder controls in the solution explorer
2. Right click on the controls folder and select Add-> new item.
3. Select Web User Control and name it customLabel
4. Get rid of the code behind files.
5. Open CustomLael.ascx file
6. Drop label control in the design mode and Type in this script and OutputCache directive in to the file
<%@ Control Language="C#" ClassName="customLabel" %>
<%@ OutputCache Duration="6" VaryByParam="*" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = System.DateTime.Now.ToString("hh:mm:ss");
}
script>
<div>
<asp:Label ID="Label1" runat="server" Text="Label1">asp:Label>
div>
7. Drag and drop this control into second row second column of the table in default aspx page.
8. Type “Page attribute for control:” in the second row first column.
9. Create another custom control in the controls folder and name in customLabel1 but this time do not delete code behind files
1. Drop a label control into the file
2. In the code behind add the PartialCaching attribute and label text value.
[PartialCaching(12)]
public partial class customLabel1 : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = DateTime.Now.ToString("hh:mm:ss");
}
}
10. Drag and drop this control into third row second column of the table in default aspx page.
11. Type “Class attribute for control:” in the third row first column.
12. Compile and execute.
13. You will notice that every time your refresh, the first label control updates but the second label control updates every 6 seconds and third label control updates every 12 seconds.
14. Now add a page level directive on the default aspx page
<%@ OutputCache Duration="3" VaryByParam="*" %>
15. Save and execute. You will notice that the first label control updates every 3 seconds, the second label control updates every 6 seconds and third label control updates every 12 seconds.
16. Sometimes will need to create areas on the page that can be updated dynamically and then integrated into a cached page. If you have a chunk of code or a method that you want to execute irrespective what the page level cache is set for, there is a new control called Substitution control to do that.
Use the Substitution control to specify a section of an output-cached Web page where you want to display dynamic content. The Substitution control offers a simplified solution to partial page caching for pages where the majority of the content is cached. You can output-cache the entire page, and then use Substitution controls to specify the parts of the page that are exempt from caching. Cached regions execute only once and are read from the cache until the cache entry expires or is purged. Dynamic regions execute every time that the page is requested. This caching model simplifies the code for pages that are primarily static, because you do not have to encapsulate the sections to cache in Web user controls. For example, this caching model is useful in a scenario where you have a page that contains static content, such as news stories, and an AdRotator control that displays advertisements. The news stories do not change frequently, which means that they can be cached. However, every time that a user requests the page, you want to display a new advertisement. The AdRotator control directly supports post-cache substitution and renders a new advertisement every time that the page posts back, regardless of whether the page is cached.
17. In the fourth row second column drag and drop the substitution control. Click on properties and type GetRealTime for MethodName property.
18. In the fourth row first column type “Substitution control:”
19. Switch to source mode and at the top of the page add this script
<script runat="server">
private static string GetRealTime(HttpContext context)
{
return "Uncached time is " + DateTime.Now.ToString();
}
script>
20. Save and execute. You will notice that the time changes for every refresh in the substitution control while the first three label controls refresh every 3, 6, and 12 seconds respectively.