How to implement fragment caching in ASP.NET?

Sheonarayan
Posted by in ASP.NET category on for Intermediate level | Points: 250 | Views : 27206 red flag
Rating: 5 out of 5  
 1 vote(s)

In this article, we shall learn how to cache the user control data or how to implement partial/fragment caching in asp.net.

Introduction

As per MSDN, A user control is a kind of composite control that works much like an ASP.NET Web page—you can add existing Web server controls and markup to a user control, and define properties and methods for the control. You can then embed them in ASP.NET Web pages, where they act as a unit.

In this article, let us learn how to implement partial/fragment caching in asp.net. This is achieved by caching the user control output. To cache the user control data, we can follow this approach.

Here is hundreds of .NET Tips and Tricks, you can also get online ASP.NET training here.

Lets first create a user control.

ASCX PAGE

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="CahceUserControl.ascx.cs"

Inherits="CahceUseControl" %>

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

 

<p><asp:Label ID="lblTime" runat="server" EnableViewState="false"

ForeColor="GradientActiveCaption" /></p>

 

ASCX CODE BEHIND

protected void Page_Load(object sender, EventArgs e)

{

lblTime.Text = DateTime.Now.ToString();

}

 

Now create a .aspx page.

ASPX PAGE

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CahcedControl.aspx.cs"

Inherits="CahcedControl" %>

<%@ Register Src="~/CahceUserControl.ascx" TagPrefix="uc1" TagName="UserControl" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

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

<head runat="server">

<title></title>

</head>

<body>

<form id="form1" runat="server">

<div>

Try to refresh this page, the Date time doesn't change. Keep refreshing and after

5 seconds you should see the change in time.

<uc1:UserControl runat="server" ID="uc11" />

</div>

</form>

</body>

</html>

In the above code snippet, we have an asp:Label control in the user control and we are writing the current Date Time in the Page_Load method of the user control. That user control is being used into the CachedControl.aspx page. So normally every time the .aspx page refreshes, the DateTime should change but as we have kept the OutputCache directives onto the user control and specified duration as "5", the User control output data will be cached for 5 seconds. So till 5 seconds of the first request even if the page is refreshed the DateTime value doesn’t change on the page. After 5 seconds the user control is processed again on the server and fresh data is cached and subsequent request till next 5 seconds is served from the Cache again.

Notice that there is another parameter in the OutputCache directive of the User Control called VarByParam that is set to "None" here, we will discuss about this parameter in our forthcoming article.

OUTPUT


In this way, we are just caching the fragment of the page. The whole page executes on the server but the user control data comes from the cache as due to OutputCache directive it doesn't executes every time.

Hope this article would be of use in understanding the partial/fragment caching in ASP.NET.

Thanks for reading, do let me know your comment or feedback. Keep reading and sharing your knowledge!

Page copy protected against web site content infringement by Copyscape

About the Author

Sheonarayan
Full Name: Sheo Narayan
Member Level: HonoraryPlatinum
Member Status: Administrator
Member Since: 7/8/2008 6:32:14 PM
Country: India
Regards, Sheo Narayan http://www.dotnetfunda.com
http://www.snarayan.com
Ex-Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001. Connect me on http://www.facebook.com/sheo.narayan | https://twitter.com/sheonarayan | http://www.linkedin.com/in/sheonarayan

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)