Date Picker control for ASP.net using Dropdown control of DD,MM,YYYY
.aspx File
<asp:DropDownList ID="ddlDay" Width="60px" runat="server">
</asp:DropDownList>
<asp:DropDownList ID="ddlMonth" Width="120px" runat="server">
</asp:DropDownList>
<asp:DropDownList ID="ddlYear" Width="80px" runat="server">
</asp:DropDownList>
<%--<asp:Image ID="imgCalendar" runat="server" ImageUrl="~/Images/calendar.gif"
EnableViewState="False"></asp:Image>--%>
<asp:Label ID="lblError" runat="server" CssClass="error" Visible="False" EnableViewState="False"><BR />The date is not valid.</asp:Label>
.cs File
.aspx File
<asp:DropDownList ID="ddlDay" Width="60px" runat="server">
</asp:DropDownList>
<asp:DropDownList ID="ddlMonth" Width="120px" runat="server">
</asp:DropDownList>
<asp:DropDownList ID="ddlYear" Width="80px" runat="server">
</asp:DropDownList>
<%--<asp:Image ID="imgCalendar" runat="server" ImageUrl="~/Images/calendar.gif"
EnableViewState="False"></asp:Image>--%>
<asp:Label ID="lblError" runat="server" CssClass="error" Visible="False" EnableViewState="False"><BR />The date is not valid.</asp:Label>
.cs File
public partial class DateTimePicker : System.Web.UI.UserControl
{
// Fields
private DateTime _dateTime;
private int _endYear = -1;
private bool _isValid;
private int _startYear = 0x7d0;
// Methods
private void BuildDropDownLists()
{
this.ddlDay.Items.Clear();
this.ddlMonth.Items.Clear();
this.ddlYear.Items.Clear();
for (int i = 1; i <= 0x1f; i++)
{
this.ddlDay.Items.Add(new ListItem(i.ToString(), i.ToString()));
}
for (int j = 1; j <= 12; j++)
{
DateTime time = new DateTime(0x7d0, j, 1);
this.ddlMonth.Items.Add(new ListItem(time.ToString("MMMM"), j.ToString()));
}
for (int k = this._startYear; k <= this.EndYear; k++)
{
this.ddlYear.Items.Add(new ListItem(k.ToString(), k.ToString()));
}
}
public override void DataBind()
{
base.DataBind();
this.ddlDay.SelectedIndex = this._dateTime.Day - 1;
this.ddlMonth.SelectedIndex = this._dateTime.Month - 1;
if ((this._dateTime.Year >= this._startYear) && (this._dateTime.Year <= this.EndYear))
{
this.ddlYear.SelectedIndex = this._dateTime.Year - this._startYear;
}
else
{
this.ddlYear.SelectedIndex = 0;
}
}
private int GetEndYear()
{
if (this._endYear != -1)
{
return this._endYear;
}
return DateTime.Now.Year;
}
private void InitializeComponent()
{
}
protected override void OnInit(EventArgs e)
{
this.InitializeComponent();
base.OnInit(e);
this.BuildDropDownLists();
}
protected void Page_Load(object sender, EventArgs e)
{
//this.imgCalendar.Attributes["onclick"] = "DisplayDatePicker('" + this.ddlDay.UniqueID + "','" + this.ddlMonth.UniqueID + "','" + this.ddlYear.UniqueID + "','" + this._startYear.ToString() + "');";
//this.imgCalendar.Attributes["style"] = "cursor: pointer;";
if (!this.Page.IsClientScriptBlockRegistered("DateTimePicker"))
{
this.Page.RegisterClientScriptBlock("DateTimePicker", "");
}
if (!base.IsPostBack)
{
this.DataBind();
}
else
{
this._startYear = Convert.ToInt32(this.ddlYear.Items[0].Value);
this._endYear = Convert.ToInt32(this.ddlYear.Items[this.ddlYear.Items.Count - 1].Value);
int day = Convert.ToInt32(this.ddlDay.SelectedItem.Value);
int month = Convert.ToInt32(this.ddlMonth.SelectedItem.Value);
int year = Convert.ToInt32(this.ddlYear.SelectedItem.Value);
try
{
this._dateTime = new DateTime(year, month, day);
if ((year >= this._startYear) && (year <= this.EndYear))
{
this._isValid = true;
}
else
{
this._isValid = false;
}
}
catch (ArgumentOutOfRangeException)
{
this._isValid = false;
}
this.lblError.Visible = !this._isValid;
}
}
// Properties
public DateTime DateTime
{
get
{
return this._dateTime;
}
set
{
this._dateTime = value;
}
}
public int EndYear
{
get
{
return this.GetEndYear();
}
set
{
this._endYear = value;
this.BuildDropDownLists();
}
}
public bool IsValid
{
get
{
return this._isValid;
}
}
public int StartYear
{
get
{
return this._startYear;
}
set
{
this._startYear = value;
this.BuildDropDownLists();
}
}
}
No comments:
Post a Comment