Here's my source code. What do you think?
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace BP.Legal.LDT.WebControls
{
///
/// Summary description for TextBoxCalendar.
///
[DefaultProperty("Text"),
ToolboxData("<{0}:TextBoxCalendar runat=server>")]
public class TextBoxCalendar : System.Web.UI.WebControls.WebControl, System.Web.UI.INamingContainer
{
protected System.Web.UI.WebControls.TextBox DateTextBox = new TextBox();
protected System.Web.UI.WebControls.LinkButton CalendarButton = new LinkButton();
protected System.Web.UI.WebControls.Image CalendarIcon = new Image();
protected System.Web.UI.WebControls.Calendar Calendar1 = new Calendar();
protected System.Web.UI.WebControls.CompareValidator DateCompareValidator = new CompareValidator();
private DateTime date;
[Bindable(true),
Category("Appearance"),
DefaultValue("")]
public DateTime DefaultDate
{
get { return this.date; }
set { this.date = value; }
}
[Bindable(true),
Category("Appearance"),
DefaultValue("")]
public string TextBoxCssClass
{
get
{
return this.DateTextBox.CssClass;
}
set
{
this.EnsureChildControls();
this.DateTextBox.CssClass = value;
}
}
[Bindable(true),
Category("Appearance"),
DefaultValue("")]
public string ButtonCssClass
{
get
{
return this.CalendarButton.CssClass;
}
set
{
this.EnsureChildControls();
this.CalendarButton.CssClass = value;
}
}
///
/// Render this control to the output parameter specified.
///
/// The HTML writer to write out to
protected override void Render(HtmlTextWriter output)
{
this.DateTextBox.RenderControl(output);
this.CalendarButton.RenderControl(output);
this.DateCompareValidator.RenderControl(output);
this.Calendar1.RenderControl(output);
}
protected override void CreateChildControls()
{
base.CreateChildControls ();
this.DateTextBox.ID = "DateTextBox";
this.Controls.Add(this.DateTextBox);
this.CalendarButton.ID = "CalendarButton";
this.CalendarButton.Click += new EventHandler(CalendarButton_Click);
this.Controls.Add(this.CalendarButton);
this.CalendarButton.Controls.Add(this.CalendarIcon);
this.Calendar1.ID = "Calendar1";
this.Calendar1.SelectionChanged += new EventHandler(Calendar1_SelectionChanged);
this.Controls.Add(this.Calendar1);
this.DateCompareValidator.ID = "DateCompareValidator";
this.DateCompareValidator.ControlToValidate = this.DateTextBox.ID;
this.DateCompareValidator.Display = ValidatorDisplay.Dynamic;
this.DateCompareValidator.ErrorMessage = "Error message goes here.";
this.DateCompareValidator.Operator = ValidationCompareOperator.DataTypeCheck;
this.DateCompareValidator.Type = System.Web.UI.WebControls.ValidationDataType.Date;
this.Controls.Add(this.DateCompareValidator);
}
private void Calendar1_SelectionChanged(object sender, EventArgs e)
{
this.date = this.Calendar1.SelectedDate;
this.DateTextBox.Text = this.date.ToShortDateString();
this.Calendar1.Visible = false;
this.DateTextBox.Visible = true;
this.CalendarButton.Visible = true;
this.DateCompareValidator.Visible = true;
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad (e);
this.EnsureChildControls();
if (! this.Page.IsPostBack)
{
this.DateTextBox.Visible = true;
if (this.date != DateTime.MinValue)
this.DateTextBox.Text = this.date.ToShortDateString();
this.CalendarButton.Visible = true;
this.Calendar1.Visible = false;
this.DateCompareValidator.Visible = true;
}
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender (e);
this.CalendarIcon.ImageUrl = this.Page.Request.ApplicationPath + System.Configuration.ConfigurationSettings.AppSettings["calendarIcon"];
}
private void CalendarButton_Click(object sender, EventArgs e)
{
if (! Page.IsValid)
return;
if (this.DateTextBox.Text == null || this.DateTextBox.Text == string.Empty)
this.date = DateTime.Today;
else
this.date = DateTime.Parse(this.DateTextBox.Text);
this.Calendar1.SelectedDate = this.date;
this.Calendar1.VisibleDate = this.date;
this.DateTextBox.Visible = false;
this.DateCompareValidator.Visible = false;
this.CalendarButton.Visible = false;
this.Calendar1.Visible = true;
this.Page.Response.Write(this.Calendar1.ID);
}
}
}