Hi friends, in
this article I will explain about how to use OutputCache directive with
Substitution or OutputCache
example program.
I already explained in the previous article about Different Types of Caching in ASP.NET | ASP.NET Page Output Cache | Improving Performance with Output Caching and Interview Questions.
Take one new website
(File—New--- website).
Take one ASP.NET
page (Root Project—Add New Item--- web form—name it as cache.aspx).
In cache.aspx
write the following code.
ASP.NET Code:
<%@ Page Language="VB"
AutoEventWireup="false"
CodeFile="cache.aspx.vb"
Inherits="cache"
%>
<%@ OutputCache duration="5"
varybyparam="none"%>
<!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 id="Head1"
runat="server">
<title>Caching</title>
</head>
<body>
<form id="form1"
runat="server">
<h1 style ="color:red">
OutPutCache example in ASP.NET </h1>
<asp:Label ID="Label1"
runat="server"
Text="Page
Cached Time" style="color:Green;font-size:20px;"></asp:Label><br />
<%= DateTime.Now.ToString() %><br /><br /><hr />
<asp:Label ID="Label2"
runat="server"
Text="Current
time or Page without caching time" style="color:Navy;font-size:20px;"></asp:Label><br />
<asp:Substitution runat="server"
ID="Substitution1"
MethodName="PresentTime"
/> <hr />
<asp:Button ID="Button1"
runat="server"
Text="Button"
style="color:red;font-size:20px;"/>
</form>
</body>
</html>
|
VB.NET Code:
Public Shared Function PresentTime(ByVal
context As HttpContext) _
As String
Return DateTime.Now.ToString()
End Function
|
C# Code:
string GetCurrentTime(HttpContext context) {
return
DateTime.Now.ToLongTimeString();
}
|
And run the
above code. It will like below.
When the button
was clicked then
When you refresh
or click on the button then only current time will be changed, the page cached
time will be changed with 5 seconds difference because we write the OutputCache
for 5 seconds.
2. Fragment caching:
Sorry for the inconvenience, I will explain the Fragment caching in the next article 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
3. Data Caching:
Sorry for the inconvenience, I will explain the Data caching in the next article.
I think you like my blog Aspdotnet-Roja why are waiting following me on facebook fan page Aspdotnet-roja
I think the admin of this site is really working hard in favor of his web page, because here every material is quality based material.
ReplyDeleteAlso visit my website: easy payday advance in Warwick
Thank You
Delete