ASP.NET Tutorial/Custom Controls/Cache

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Cache user control (Fragment Caching With Property)

<%@ 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";
   }
 }


Cache with user component

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="CacheTest" %>
<%@ Register Src="Control.ascx" TagName="CachedWebUserControl" TagPrefix="uc1" %>
<!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>Cache Test</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
        <uc1:CachedWebUserControl id="CachedWebUserControl1" runat="server">
        </uc1:CachedWebUserControl>&nbsp;</div>
    </form>
</body>
</html>
File: Default.aspx.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 CacheTest : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    this.Label1.Text = string.Format("The Time in the Web Page is: {0}",
      DateTime.Now.ToLongTimeString());
    }
}

File: Control.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Control.ascx.cs" Inherits="CachedWebUserControl"  %>
<%@ OutputCache Duration=60 VaryByParam="none" %>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
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 CachedWebUserControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
    this.Label1.Text = string.Format("The Time in the User Control is: {0}",
      DateTime.Now.ToLongTimeString());
    }
}