Thursday, May 3, 2012

Substitution Control

How you can use SubstitutionControl  in Asp.net?

Use the Substitution control to specify a section of an output-cached Web page where you want to display dynamic content. The Substitution control offers a simplified solution to partial page caching for pages where the majority of the content is cached. You can output-cache the entire page, and then use Substitution controls to specify the parts of the page that are exempt from caching. Cached regions execute only once and are read from the cache until the cache entry expires or is purged. Dynamic regions execute every time that the page is requested. This caching model simplifies the code for pages that are primarily static, because you do not have to encapsulate the sections to cache in Web user controls. For example, this caching model is useful in a scenario where you have a page that contains static content, such as news stories, and an AdRotator control that displays advertisements. The news stories do not change frequently, which means that they can be cached. However, every time that a user requests the page, you want to display a new advertisement. The AdRotator control directly supports post-cache substitution and renders a new advertisement every time that the page posts back, regardless of whether the page is cached.

Aspx Code
 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Substitution-control.aspx.cs" Inherits="Authentication_Authorization.Substitution_control" %>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
 <%@ OutputCache Duration="60" VaryByParam="None" %>  
 <html xmlns="http://www.w3.org/1999/xhtml">  
 <head runat="server">  
   <title></title>  
 </head>  
 <body>  
   <form id="form1" runat="server">  
   <div>  
   <p>  
   Asp Label<br />  
   <asp:Label runat="server" ID="Label1" />  
   </p>  
   <p>Substitution-Control<br />  
   <asp:Substitution runat="server"  
     ID="Substitution1"  
     MethodName="GetTime" />  
   </p>  
   <p>  
   <asp:Button runat="server" ID="Button1" Text="Submit"/>  
   </p>  
   </div>  
   </form>  
 </body>  
 </html>  
 Code Behind (.cs)  
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Web;  
 using System.Web.UI;  
 using System.Web.UI.WebControls;  
 namespace Authentication_Authorization  
 {  
   public partial class Substitution_control : System.Web.UI.Page  
   {  
     protected void Page_Load(object sender, EventArgs e)  
     {  
       Label1.Text = DateTime.Now.ToString();  
     }  
     public static String GetTime(HttpContext context)  
     {  
       return DateTime.Now.ToString();  
     }  
   }  
 }  

No comments:

Post a Comment