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

Fragment Caching in Asp.Net with Example

Aug 8, 2016

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

In previous articles I explained connection pooling in, 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# /, page load vs page init with example
and many articles relating to, c#,, JavaScript and jQuery. Now I will explain what is fragment caching in and how to use fragment caching in using c#, 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 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.


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


Following is the demo of fragment caching in example.

Fragment Caching in Asp.Net with Example

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

Get the latest,, 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


Unknown said...

This is most valuable article.

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

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