Cache specific section of Page in ASP.Net using C# and VB.Net

Last Reply 6 months ago By pandeyism

Posted 6 months ago

i want to cache the portion of a page instead of whole page?

Posted 6 months ago

I will get back soon.

Posted 6 months ago

Hey khatri852,

Please refer below sample.


<%@ OutputCache Duration="5" VaryByParam="none" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head id="Head1" runat="server">
    <title>Substitution Class Example</title>
    <form id="form1" runat="server">
        This section of the page is not cached:</p>
    <asp:Substitution ID="Substitution1" MethodName="GetCurrentDateTime" runat="Server">
    <br />
        This section of the page is cached:</p>
    <asp:Label ID="lblDate" runat="Server">


protected void Page_Load(object sender, EventArgs e)
    lblDate.Text = DateTime.Now.ToString();

public static string GetCurrentDateTime(HttpContext context)
    return DateTime.Now.ToString();


Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    lblDate.Text = DateTime.Now.ToString()
End Sub

Public Shared Function GetCurrentDateTime(ByVal context As HttpContext) As String
    Return DateTime.Now.ToString()
End Function