Caching whole page except a user control in ASP.Net application

Approach 1)

As described in the following figure, the page is divided into 4 user controls, 3 of them are cached and 1 of the control that you want to load dynamically is not cached.

Gotcha: In this approach we have to make sure that we do not add the output cache directive (<%@ OutputCache Duration="86400" VaryByParam="none" %>) to the page.

User control 1

<%@ OutputCache Duration="86400" VaryByParam="none" %>

User control 4 (Not Cached)

<%@ OutputCache Duration="86400" VaryByParam="none" %>

User control 3

<%@ OutputCache Duration="86400" VaryByParam="none" %>

User control 4

<%@ OutputCache Duration="86400" VaryByParam="none" %>

Approach 2)

In this approach you can use server control and add substitution API to call a static method on the page to get dynamic string that will be displayed in the control.

This way the whole page is cached but the control is displaying the dynamic data.

ASPX

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MembersHome.aspx.cs" Inherits="MemberLogin" %>

<%@ OutputCache Duration="86400" VaryByParam="none" %>

<html xmlns="http://www.w3.org/1999/xhtml"><body>

<asp:Substitution runat="server" ID="idSubstitution" MethodName="GetVerificationImage" />

body>html>

ASPX.CS

public partial class MemberLogin : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e){}

public static string GetVerificationImage(HttpContext context)

{

return CommonFunctions.GenerateRandomString();

}

}

References:

Dynamically Updating Portions of a Cached Page

http://msdn.microsoft.com/en-us/library/ms227429.aspx

Substitution Class

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.substitution.aspx

ASP.NET Caching

http://msdn.microsoft.com/en-us/library/ms972379.aspx

Comments

Popular posts from this blog

WPF How to Dispose ViewModel when the associated UserControl (Not Window) closes?

C# How to unit test Dispatcher

WPF: How to Deep Copy WPF object (e.g. UIElement) ?