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"ASCX CODE BEHIND
Inherits="CahceUseControl" %>
<%@ OutputCache Duration="5" VaryByParam="None" %>
<p><asp:Label ID="lblTime" runat="server" EnableViewState="false"
ForeColor="GradientActiveCaption" /></p>
protected void Page_Load(object sender, EventArgs e)Now create a .aspx page.
{
lblTime.Text = DateTime.Now.ToString();
}
ASPX PAGE
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CahcedControl.aspx.cs"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.
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>
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 toOutputCache 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!
No comments :
Post a Comment