(c) 2002 Visual Studio Magazine 
Fawcette Technical Publications

Issue: June 2002
Section: ASP.NET
Author: Chris Kinsman

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);
		}
	}
}

C#	Render a Control With HtmlTextWriter
Listing 2	You can use HtmlTextWriter to render a control instead of output.Write(). This code also uses AddAttribute to alter the table's Border attribute.

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

namespace ServerControls
{
	/// <summary>
	/// Summary description for SimpleTable.
	/// </summary>
	public class SimpleTable : 
		System.Web.UI.WebControls.WebControl
	{

		/// <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 the table
			output.AddAttribute
				(HtmlTextWriterAttribute.Border, 
				"1");
			output.AddStyleAttribute
				(HtmlTextWriterStyle.BorderStyle, 
				"groove");
			output.RenderBeginTag(HtmlTextWriterTag.Table);
			// Output the header
			output.RenderBeginTag(HtmlTextWriterTag.Tr);
			output.AddAttribute
				(HtmlTextWriterAttribute.Align, 
				"center");
			output.RenderBeginTag(HtmlTextWriterTag.Td);
			output.RenderBeginTag(HtmlTextWriterTag.B);
			output.Write("Place");
			output.RenderEndTag();
			output.RenderEndTag();
			output.AddAttribute
				(HtmlTextWriterAttribute.Align, 
				"center");
			output.RenderBeginTag(HtmlTextWriterTag.Td);
			output.RenderBeginTag
				(HtmlTextWriterTag.B);
			output.Write("Name");
			output.RenderEndTag();
			output.RenderEndTag();

			// Output the rows
			for(int iCount=1; iCount <=10; 
				iCount++)
			{
				output.RenderBeginTag(HtmlTextWriterTag.Tr);
				output.RenderBeginTag
					(HtmlTextWriterTag.Td);
				output.Write(iCount.ToString());
				output.RenderEndTag();
				output.RenderBeginTag(HtmlTextWriterTag.Td);
				output.Write("Test Row");
				output.RenderEndTag();
				output.RenderEndTag();
			}

			output.RenderEndTag();
			output.RenderEndTag();
		}
	}
}

C#	Use Composition to Render a Server Control
Listing 3	You can use composition to render a Server Control instead of overriding the Render methods. This code takes advantage of the Controls collection to handle rendering. Several controls are added to the collection and ASP.NET handles the rendering.


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

namespace ServerControls
{
	/// <summary>
	/// Summary description for CompositionTable.
	/// </summary>
	public class CompositionTable : 
		System.Web.UI.WebControls.WebControl
	{
		public CompositionTable()
		{
			//EnsureChildControls();
		}

		protected override void 
			CreateChildControls()
		{
			// Create the table
			HtmlTable ht = new HtmlTable();
			ht.Border = 1;
			ht.Style.Add("BorderStyle", "groove");
			// Add it to the controls collection
			this.Controls.Add(ht);

			// Create the header row
			HtmlTableRow htr = new HtmlTableRow();
			ht.Rows.Add(htr);
			HtmlTableCell htc = new 
				HtmlTableCell();
			htc.InnerHtml = "<B>Place</B>";
			htc.Align = "center";
			htr.Cells.Add(htc);
			htc = new HtmlTableCell();
			htc.InnerHtml = "<b>Name</b>";
			htc.Align = "center";
			htr.Cells.Add(htc);

			for(int iCount = 1; iCount <= 10; 
				iCount++)
			{
				htr = new HtmlTableRow();
				ht.Rows.Add(htr);
				htc = new HtmlTableCell();
				htc.InnerText = iCount.ToString();
				htr.Cells.Add(htc);
				htc = new HtmlTableCell();
				htc.InnerText = "Test Row";
				htr.Cells.Add(htc);
			}
		}
	}
}
