ASP.NET Extender Control for Expiring Content

I recently had a specific requirement to ‘expire’ some content on a web site.  There’s a million ways to do this, but my previous use of ExtenderControl in Windows Forms development sparked my interest when I noticed the Ajax toolkit supports this (mainly for client-side functionality). 


After downloading System.Web.Extensions, and about half an hour later I’d got the ‘ExpiringExtender’ Control that can be added to your ASP.NET (2.0+) page to allow content to phase in or out. 


The only down side to this approach is that your source code becomes littered with expired code like the Earth’s orbit does with old satellites!  This was therefore a quick solution to a specific problem – not an approach I’d advocate if this is a core requirement (you need a good Content Management System with built-in scheduling).


Anyway – here’s the class (chuck it into your App_Code folder if you’re using a Web Site project – as in my example).  The code’s pretty simple – just set a couple of properties (defaulting to min and max dates if not supplied) and the control’s visible property gets set appropriately.


using System;
using 
System.Web;
using 
System.Web.UI;
using 
System.Web.UI.WebControls;


namespace 
CodeBureau.WebControls
{


    
/// <summary>
    /// This control works as a simple Extender to any server or html control to allow crude time-based 
    /// show-hide functionality.  NOTE This requires the Ajax Toolkit.
    /// </summary>
    
[TargetControlType(typeof(Control))]
    
public class ExpiringExtender : ExtenderControl
    {

        
#region Private Attributes

        
private DateTime showFrom DateTime.MinValue;
        private 
DateTime showTo DateTime.MaxValue;

        #endregion

        #region
 Public Attributes

        
/// <summary>
        /// Gets or sets the show from date.  This indicates that the control should be visible from this date/time
        /// </summary>
        /// <value>The show from.</value>
        
public DateTime ShowFrom
        {
            
get return this.showFrom}
            
set this.showFrom = value; }
        }

        
/// <summary>
        /// Gets or sets the show to date.  This indicates that the control should be visible to this date/time
        /// </summary>
        /// <value>The show to.</value>
        
public DateTime ShowTo
        {
            
get return this.showTo}
            
set this.showTo = value; }
        }

        
#endregion

        #region
 Event Handlers

        
/// <summary>
        /// Raises the <see cref=”E:PreRender”/> event.
        /// </summary>
        /// <param name=”e”>The <see cref=”System.EventArgs”/> instance containing the event data.</param>
        
protected override void OnPreRender(EventArgs e)
        {
            
base.OnPreRender(e);

            
Control control FindControl(this.Page, this.TargetControlID);
            if 
(control != null)
            {
                DateTime now 
DateTime.Now;
                if 
(now < showFrom || now > showTo)
                {
                    control.Visible 
= false;
                
}
            }

        }

        
#endregion        

        
/// <summary>
        /// Gets the script descriptors.
        /// </summary>
        /// <param name=”targetControl”>The target control.</param>
        /// <returns></returns>
        
protected override System.Collections.Generic.IEnumerable<ScriptDescriptor> GetScriptDescriptors(Control targetControl)
        {
            
return null;
        
}

        
/// <summary>
        /// Gets the script references.
        /// </summary>
        /// <returns></returns>
        
protected override System.Collections.Generic.IEnumerable<ScriptReference> GetScriptReferences()
        {
            
return null;
        
}

        
/// <summary>
        /// Finds the given control (recursively).
        /// </summary>
        /// <param name=”parent”>The parent.</param>
        /// <param name=”id”>The id.</param>
        /// <returns>the control if found (else null)</returns>
        
private Control FindControl(Control parent, string id)
        {
            Control recurse
;
            if 
(parent.ID == id)
            {
                
return parent;
            
}

            
foreach (Control child in parent.Controls)
            {
                recurse 
FindControl(child, id);
                if 
(recurse != null)
                {
                    
return recurse;
                
}
            }
            
return null;
        
}

    }

}




Colorized by: CarlosAg.CodeColorizer


And to use it in a page (I’ve got it within a master page here) you just add the registration for the extender control (note the Assembly=”App_Code” – I had to google for a while to find that.


You also have to add the ScriptManager control (an Ajax requirement), then just make sure the content you want to phase in or out has runat=”server” (it should work for HTML or Server controls).


<%@ Register Assembly=”App_Code” Namespace=”Coles.WebControls”  TagPrefix=”uc1″ %>


<asp:Content ID=”Content2″ ContentPlaceHolderID=”body” Runat=”Server”>
<asp:ScriptManager id=”ScriptManager1″ runat=”server”></asp:ScriptManager>
    
<uc1:ExpiringExtender runat=”server” ID=”a” TargetControlID=”header” ShowTo=”13 March 2008 16:15:00″></uc1:ExpiringExtender>

<h1 id=”header” runat=”server”>Hey – I turn into a pumpkin at 4:15…</h1>



Colorized by: CarlosAg.CodeColorizer