Cache user control (Fragment Caching With Property) : Cache « Custom Controls « ASP.NET Tutorial






<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="Default_aspx" %>
<%@ Register TagPrefix="MyUserControl"  TagName="LoadTime" Src="~/Control.ascx"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Fragment Caching</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h1>Fragment Caching</h1>
     <asp:Label ID="lblTime" runat="server" />
     <br />
     <MyUserControl:LoadTime runat="server" />
    </div>
    </form>
</body>
</html>
File: Default.aspx.cs


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Default_aspx : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
     lblTime.Text = "This page was loaded at " +
       DateTime.Now.ToLongTimeString();
    }
}


File: Control.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Control.ascx.cs" Inherits="SimpleUserControl_ascx" %>
<%@ OutputCache Duration="10" VaryByParam="*" %>

<hr />
<h3>User Control</h3>
<asp:Label ID="lblTime" runat="server" />
<br />
<asp:Label ID="lblUserName" runat="server" Text="Dan" />
<br />
<asp:Button ID="btn" runat="server" Text="Change Name to Jesse" OnClick="btn_Click" />
<hr />

File: Control.ascx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class SimpleUserControl_ascx : System.Web.UI.UserControl
{
  public string UserName
  {
    get
    {
      return lblUserName.Text;
    }
    set
    {
      lblUserName.Text = value;
    }
  }

  protected void Page_Load(object sender, EventArgs e)
    {
     lblTime.Text = "This user control was loaded at " +
       DateTime.Now.ToLongTimeString();
    }
   protected void btn_Click(object sender, EventArgs e)
   {
     lblUserName.Text = "Jesse";
   }
 }








14.6.Cache
14.6.1.Cache with user component
14.6.2.Cache user control (Fragment Caching With Property)