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

Page Output Caching in ASP.NET

Author: Richard Wand

Page Output Caching has the great importance when you have static pages. “@ OutputCache” directive has several attributes that provide flexibility on Page Output Caching.

 

Caching in ASP.NET was explained with its different types in a previous article on DevASP.NET. This article will demonstrate the entire page OutputCache and its attributes with an example.

 

Open Visual Studio 2010, go to File from menu > New > Web Site
Create ASP.NET Empty Web Site either in VB.NET or C#
Go to Website from menu > Add New Item > Web Form  and Click on Add
 
First we are going to see “Duration” attribute effect of OutputCache
 
  1. Add following “@ OutputCache” directive at second line below the “@ Page” directive in Default.aspx page’s source view.

    <%@ OutputCache Duration="20" VaryByParam="None"%>
     
    We have set the “Duration” attribute to “20” which means that OutputCache will expire after 20 seconds. “VaryByParam” attribure is set to “None”. “VaryByParam” attribute is explained later in the article.
     
  2. Write code below in between <div> tag in Default.aspx page.

    <h2>Output Cache Example, Duration: 20 seconds</h2>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
     
  3.  Now write code below in Page Load method of code behind file Default.aspx.cs or Default.aspx.vb

    C#
     
    Label1.Text = "Now the Time is: " + DateTime.Now.ToString();
     
    VB.NET
     
    Label1.Text = "Now the Time is: " & DateTime.Now.ToString()
     
  4. Press “F5” or “Ctrl + F5”
  5. Refresh the page to see result. The time is cached and will not change until the time expired. New current time will display after 20 seconds.

 

Caching on Client side (“Location” attribute)

You can set the location attribute in “@ OutputCache” directive. The location can be “Any”, “Client”, “Downstream”, “Server”, “None” or “ServerAndClient”.  Default is set to “Any”. Both server and client side output caching has its advantages. Mostly server side output caching is used but there can be some situations where we need to specify client side output caching. Below is the example to use “Location” attribute to set caching on client side.
 
  1. Just Add “Location” attribute and set its value to “Client” (like below) in the “@ OutputCache” directive which we have added in Default.aspx page.

    <%@ OutputCache Duration="20" VaryByParam="None" Location="Client" %>

     
  2. Save file, build website and press “F5” or “Ctrl + F5”
  3. Client attribute will not show its effect when you refresh the page. Copy the URL, paste it in another window of browser and press enter. You will see cached time now.

 

Caching Query String Parameters (“VaryByParam” attribute)

“VaryByParam” is a required attribute of “@ OutputCache” directive. A parser error will occur without this attribute. Output Caching can vary by query string parameters. If we set the value of “VaryByParam” attribute to “Param” then Output cache will depend on the value of parameter “Param”. When a user requests the page first time, first copy of the page will be cached. If another request is sent with a query string parameter value, the second copy of the page will be cached. In the same way with another query string parameter value, third copy of the page will be cached. Now if the user requests the page with first query string parameter value and specified cache duration is not expired the cached copy of first query string parameter value will return. This concept is simplified in below example.
 
  1. Change “VaryByParam” attribute value from “None” to “Param” in “@ OutputCache” directive like below

    <%@ OutputCache Duration="20" VaryByParam="Param" %>
     
  2.  Write code below in between <div> tag of Default.aspx page

    <h2>Output Cache Example, Duration: 20 seconds</h2>
       
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
       
    <br />
       
    <a href="?Param=1">First</a>
       
    <br />
       
    <a href="?Param=2">Second</a>
       
    <br />
       
    <a href="?Param=3">Third</a>
     
  3.  Write code below in Page Load method of code behind file Default.aspx.cs or Default.aspx.vb

    C#
     
    Label1.Text = "Now the Time is: " + DateTime.Now.ToString();
     
    VB.NET
     
    Label1.Text = "Now the Time is: " & DateTime.Now.ToString()

     
  4. Save file, build website and press “F5” or “Ctrl + F5”
  5. Click on First and note the displayed time. Click on Second and note the displayed time. Click again on First. You will see the cached time if duration of 20 seconds is not expired.
 
“VaryByControl” Attribute

Output caching can vary by a specific control values. For example it can dependent on a DropDownList control items. Below example will show the “VaryByControl” attribute effect with DropDownList control.

 

  1. Write code below in between <div> tag of Default.aspx page

    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
           <br />
           <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
               onselectedindexchanged="DropDownList1_SelectedIndexChanged">
           <asp:ListItem>First</asp:ListItem>
           <asp:ListItem>Second</asp:ListItem>
           <asp:ListItem>Third</asp:ListItem>
    </asp:DropDownList>

    I have added a Label control, a DropDownList Control and added three List Items in DropDownList.    
     
  2. Now add “VaryByControl” attribute in “@ OutputCache” directive like below

    <%@ OutputCache Duration="20" VaryByParam="None" VaryByControl="DropDownList1" %>

     
  3. Write code below code behind file Default.aspx.cs or Default.aspx.vb

    C#
     
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) {
            Label1.Text = "Now the Time is: " + DateTime.Now.ToString();
           }
     
    VB.NET
     
    Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
     
            Label1.Text = "Now the Time is: " & DateTime.Now.ToString()
     
    End Sub
     
  4. Save file, build website and press “F5” or “Ctrl + F5”
  5. Select Second from Drop down list and note the time. Select third item and note the time. Now select again second item. You will see the cached time if the time duration of 20 seconds is not expired.

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Caching in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net