Hi friends, in
this article I will explain about Fragment Caching in ASP.NET or How To Perform
Fragment Caching in ASP.NET by Using VB.NET/C# or Fragment Caching Example, Page
Fragment Caching in ASP.NET.
What is Fragment Caching in ASP.NET?
- Fragment Caching is used to the caching of individual user controls within a Web Form.
- Each user control can have independent cache durations and implementations of how the caching behavior is to be applied.
- Fragment caching is useful when you need to cache the some part of a page like user control.
- Navigation bars, header, and footers are good examples for fragment caching.
- Fragment caching allows to cache specific portions of the page rather than the whole page. It is done by implementing the page in different parts by creating everything in form of user controls and caching each user control individually.
Create website (File
---- New website ----give name to website).
Take one Web User Control (how to create webuser control) and name it as fragment.aspx.
And right the
following code in source.
<%@ Control Language="VB"
AutoEventWireup="false"
CodeFile="fragment.ascx.vb"
Inherits="fragment"
%>
<%@ OutputCache Duration="5"
VaryByParam="None"
%>
<p><asp:Label ID="lblTime" runat="server" EnableViewState="false" ForeColor="green" /></p>
|
In Code behind
write following code.
In VB.NET code
Partial Class
fragment
Inherits System.Web.UI.UserControl
Protected Sub
Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
lblTime.Text = DateTime.Now.ToString()
End Sub
End Class
|
In
C#:
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
partial class
fragment : System.Web.UI.UserControl
{
protected void Page_Load(object
sender, System.EventArgs e)
{
lblTime.Text
= DateTime.Now.ToString();
}
Public fragment()
{
Load
+= Page_Load;
}
}
|
Take one web
form and name it as FragmentCaching.aspx
In Source code
write the following code.
<%@ Page Language="VB"
AutoEventWireup="false"
CodeFile="FragmentCaching.aspx.vb"
Inherits="FragmentCaching"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Register Src="~/fragment.ascx"
TagPrefix="usrcntl"
TagName="changetime"
%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1"
runat="server">
<title>Aspdotnet-Roja:Fragment caching Example</title>
</head>
<body>
<form id="form1"
runat="server">
<div>
<h1 style="color:Red">Fragment
caching Example</h1>
<h5> Refresh the Page,the time doesn't change. Keep
refreshing and after 5 seconds you should see the change in time.</h5>
<usrcntl:changetime runat="server"
ID="uc11"
/>
</div>
</form>
</body>
</html>
|
The output of
the page is as follows.
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 my Aspdotnet-Roja blog, please give me your
comment or feedback. Keep reading and sharing your knowledge!
Useful...
ReplyDeleteThank U Priya
Delete