C#  •  Insert a Time Stamp

 

Listing 1. This simple server control inserts a time stamp into the page. The server control shows the time stamp at design time instead of just a gray box as a user control would.

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

namespace ServerControls
{
   [DefaultProperty("Text"), 
      ToolboxData("<{0}:TimeStamp 
         runat=server></{0}:TimeStamp>")]
   public class TimeStamp : 
      System.Web.UI.WebControls.WebControl
   {
      private string text = 
         DateTime.Now.ToString();
   
      [Bindable(true), Category("Appearance"), 
         DefaultValue("")] 
      public string Text 
      {
         get
         {
            return text;
         }

         set
         {
            text = value;
         }
      }

      /// <summary> 
      /// Render this control to the output 
         parameter specified.
      /// </summary>
      /// <param name="output"> The HTML writer 
         to write out to </param>
      protected override void 
         Render(HtmlTextWriter output)
      {
         output.Write(Text);
      }
   }
}