Sunday, 18 August 2013

How to use OutputCache directive with Substitution in ASP.NET || ASP.NET Page OutputCache Example

Hi friends, in this article I will explain about how to use OutputCache directive with Substitution or OutputCache example program.
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:
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

2 comments:

  1. 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.


    Also visit my website: easy payday advance in Warwick

    ReplyDelete

© 2012-2018 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.