Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

Partial Page Caching in ASP.NET

Author: Richard Wand

Partial page caching is done by adding a user control in a page with OutputCache directive in user control.

Partial Page Caching is explained in a previous article on DevASP.NET. It is also called fragment caching as only the portion of page will be cached instead of whole page output caching.

We will see Partial Page Caching in this article with Web User Control.


Web User Control
A “Web User Control” is much like a Web Form with some fundamental differences. You can add any HTML or Server Control to Web User Control as you can in ASP.NET Page. It has the user interface file as well as code behind file like Web Forms. You can do any functionality in code behind file of Web User Control.
Web User Controls are different from Web Forms in a way that they cannot run independently. Web User Controls must be included in ASP.NET page like we add Server Controls. They are also different because they use .ascx extension and they have “@ Control” directive instead of “@ Page” directive. Web User Control file don’t have <html>, <body> and <form> tags because these tags will exists in aspx page that will contain this Web User Control file.
Partial page caching with web server control is also called control caching.
  1. Open Visual Studio 2010, go to File from menu > New > Web Site
  2. Create ASP.NET Empty Web Site either in VB.NET or C#
  3. Go to Website from menu > Add New Item > Web Form  and Click on Add
  4. We have to add Web User Control for Partial Page Caching
  5. Go to Website from menu again > Add New Item > Web User Control  and Click on Add
  6. You can see that Web User Control file has .ascx extension and it has “@ Control” directive instead of “@ Page” directive. It also has “CodeFile” attribute like aspx page.
  7. Write “@ OutputCache” directive in WebUserControl.ascx page under “@ control” directive at second line.

    <%@ OutputCache Duration="20" VaryByParam="None" %>
  8.  You can see there is no <html>, <body> and <form> tags in ascx file. Write code below in ascx file under “@ OutputCache” directive.

    <h2>This is User Control, User Control Cache duration 20 seconds</h2>
    <br />
     <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

  9.  Open WebUserControl.ascx.cs or WebUserControl.ascx.vb file and write code below in Page Load method.

    Label1.Text = "Time in User Control is: " + DateTime.Now.ToString();
    Label1.Text = "Time in User Control is: " & DateTime.Now.ToString()
  10. Now open Default.aspx file and add “@ OutputCache” directive under “@ Page” directive.

    <%@ OutputCache Duration="10" VaryByParam="None" %>

    Duration for Output Page Caching is set to 10 seconds while duration for Partial Page Caching for User Control is set to 20 seconds. (See Number 7)  
    This “@ OutputCache” directive is for entire page caching. Partial Page Caching with Web User Control has no effect if we don’t include “@ OutputCache” directive in aspx page.  Web User Control has its own “@ OutputCache” directive in ascx file.
  11. We have to add “@ Register” directive in aspx page to register our User Control. Write below “@ Register” directive line under “@ OutputCache” directive.

    <%@ Register TagPrefix="SampleControl" TagName="Sample" Src="WebUserControl.ascx" %>

    We have to add “TagPrefix” and “TagName” attributs for the user control. For this example, I have used “SampleControl” for TagPrefix and “Sample” for TagName. In “Src” attribute, we have to provide the relative or absolute URL of Web User Control file. For this example, file name is “WebUserControl.ascx”.
  12. Now add code below in between <div> tag of Default.aspx page.

    <h2>Partial Page Example: Page Cache duration 10 seconds</h2>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <br />
    <SampleControl:Sample runat="server" />

    I have added a <h2> tag and a Label control. To add User Control just add <SampleControl:Sample> tag. Remember, SampleControl is the TagPrefix and Sample is TagName that we have mentioned in “@ Register” directive. (See Number 11)
  13.  Now Open Default.aspx.cs or Default.aspx.vb file and write code below in Page Load method.

    Label1.Text = "Time in Page is: " + DateTime.Now.ToString();
    Label1.Text = "Time in Page is: " & DateTime.Now.ToString()

  14. Save all files, build website and press “F5” or “Ctrl + F5”
  15. Refresh Page, you will see that time in user control will be cached for 20 seconds while time in page will be cached for 10 seconds.

 Partial page caching is powerful way of caching. We can separate static part of the page from dynamic part.

Add Article Comment:
Name :
Email Address :
Comments :
<< What are Cache Profiles and how to use cache profiles in ASP.NET?

Disclaimer - Privacy
© 2002-2017