Caching User Controls

9/23/2010 3:08:46 PM
Just as whole pages can be cached, ASP.NET supports caching UserControls as well. Imagine your job is to create a sizable Web site that allows users to navigate through information using various navigation controls (menus, hyperlinks, and so forth). For example, imagine a part of your page shows links or other navigation controls that lead users to the most recent news, summary information, and other places. The actual content might change, but the links probably don't. If the links don't change very often and the cost of generating that section of the page is expensive, it makes sense to move the functionality into a UserControl and apply the OutputCache directive to the UserControl. Doing so causes ASP.NET to cache the portion of the page represented by the control.

The OutputDirective can be applied to the ASCX file that makes up a UserControl. The OutputDirective for a UserControl can also use the Shared property to tell ASP.NET to cache one version of the control for all pages that use it, resulting in potentially even higher performance over the span of many hits (the default is false).

The following exercise illustrates how to cache the output of a UserControl.

Caching the output of user controls

  1. Create a simple UserControl for the OutputCaching project. Navigation controls are perfect for caching, so create a control that has a menu. Name the control SiteMenu.ascx. Drag a Menu control onto the UserControl, as shown here:

    Add some menu items, as shown in this graphic:

  2. Add the OutputCache directive with the following parameters in the control source, like so:

    <%@ Control Language="C#" AutoEventWireup="true"
    CodeFile="SiteMenu.ascx.cs" Inherits="SiteMenu" %>
    <%@ OutputCache Duration="60" VaryByParam="none" %>

  3. Create a new page in the project. Name it UseSiteMenuControl.aspx.

  4. Drag the SiteMenu UserControl from Solution Explorer onto the UseSiteMenuControl page. When ASP.NET loads and runs your Web page, ASP.NET caches the UserControl because the UserControl mentions the OutputDirective.

  5. Make sure tracing is turned on in the UseSiteMenuControl.aspx file. (That is, set the Trace="true" attribute in the Page directive.) Surf to the page. The first time you surf to the page, you'll see the following information in the control tree section of the Trace output:

    Notice that the entire control tree was rendered. Press the refresh key (F5 in Internet Explorer) while looking at UseSiteMenuControl.aspx. Examine the control tree portion of the Trace output again. Notice that ASP.NET uses the cached control instead of rerendering the entire SiteMenu control.

Most View
Improve Your Mac (Part 5) - Using little snitch to identify bandwidth hogs
Year End 2012 - Reviews & Rankings (Part 1)
Adobe Creative Cloud - Adobe’s Big Gamble (Part 1)
T-Mobile’s Samsung Galaxy Note II Review (Part 3)
SQL Server 2012 : SQL Server Management and Development Tools - Object Explorer (part 2)
All You Need To Know About iOS 6 (Part 3)
Powershot SX280 HS Review - Canon’s Latest Travel Zoom Camera Model (Part 1)
Windows Server 2008 : Working with Active Directory Accounts - Using ldifde to Export, Import, and Delete Accounts
System Center Configuration Manager 2007 : Creating Packages (part 2) - Comparing GPO-based Software Distribution to ConfigMgr Software Distribution
Video Goes Personal (Part 2) : Harman/Kardon CL, Micromax LED42K316, BENQ LR100
Top 10
Sharepoint 2013 : Farm Management - Disable a Timer Job,Start a Timer Job, Set the Schedule for a Timer Job
Sharepoint 2013 : Farm Management - Display Available Timer Jobs on the Farm, Get a Specific Timer Job, Enable a Timer Job
Sharepoint 2013 : Farm Management - Review Workflow Configuration Settings,Modify Workflow Configuration Settings
Sharepoint 2013 : Farm Management - Review SharePoint Designer Settings, Configure SharePoint Designer Settings
Sharepoint 2013 : Farm Management - Remove a Managed Path, Merge Log Files, End the Current Log File
SQL Server 2012 : Policy Based Management - Evaluating Policies
SQL Server 2012 : Defining Policies (part 3) - Creating Policies
SQL Server 2012 : Defining Policies (part 2) - Conditions
SQL Server 2012 : Defining Policies (part 1) - Management Facets
Microsoft Exchange Server 2010 : Configuring Anti-Spam and Message Filtering Options (part 4) - Preventing Internal Servers from Being Filtered