Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Fragment Caching in Asp.Net with Example

Aug 8, 2016
Introduction

Here I will explain what is fragment caching in
asp.net or partial caching in asp.net with examples or how to use partial / fragment caching in asp.net using c# with example. Fragment caching in asp.net allows us to cache particular portion of web page instead of whole page and this can be achieved by using user controls in asp.net.

Description:
  
In previous articles I explained connection pooling in asp.net, difference between initialization and instantiation, SQL difference between @@Identity, scope_identity, ident_current, cursor in sql server with example, dispose vs finalize method in c# / vb.net, asp.net page load vs page init with example
and many articles relating to asp.net, c#, vb.net, JavaScript and jQuery. Now I will explain what is fragment caching in asp.net and how to use fragment caching in asp.net using c#, vb.net with example.

What is Fragment Caching in Asp.Net?

Fragment caching means cache only particular portion of the web page. This fragment caching can be achieved by using user controls (.ascx) in web form. In asp.net for each user control we can set cache durations separately.

To achieve fragment caching first create one user control (.ascx) in your application for that Right click on your application --> Select Add --> Add New Item --> Select Web User Control --> Give name and Click OK.

Now open user control and write the code like as shown below.

WebUserControl.ascx


<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
<%@ OutputCache Duration="5" VaryByParam="None" %>

<asp:Label ID="lblTime" runat="server" Font-Bold="true" ForeColor="Blue" />

If you observe above code here we are caching user control for “5” seconds by using “OutputCache” property. This user control values will update only after every “5” seconds even if continuously refresh page. Now open WebUserControl.ascx.cs code behind file and write the following code


protected void Page_Load(object sender, EventArgs e)
{
lblTime.Text = DateTime.Now.ToString();
}

Now drag and drop user control to your aspx page in Design mode or write the code like as shown below to add user control to aspx page.


<%@ Register src="WebUserControl.ascx" tagname="WebUserControl" tagprefix="uc1" %>
<!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>Fragment Caching in Asp.Net with Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>Refresh the Page Time Will Update Only After Every 5 Seconds</p>
<uc1:WebUserControl ID="WebUserControl1" runat="server" />
</div>
</form>
</body>
</html>

Demo

Following is the demo of fragment caching in asp.net example.

Fragment Caching in Asp.Net with Example


If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

2 comments :

Azher said...

Nice Article and very helpful

Rahul Pundir said...

This is most valuable article.

Give your Valuable Comments

Other Related Posts

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.