DayMonthYear.aspx
<%@ Page Language="C#" %>
<html>
<head runat="server">
<title>
Select Year Month and Day from drop down list in ASP.NET
</title>
</head>
<body>
<form id="form1" runat="server">
<table class="chartoptions">
<tr>
<td>
<label>Select Year:</label>
</td>
<td>
<asp:DropDownList ID="ddlYear" runat="server" AutoPostBack="true" Style="width: 90px;"
OnSelectedIndexChanged="ddlYear_SelectedIndexChanged">
<asp:ListItem Selected="True" Value="Select">Select</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
<label>Select Month:</label>
</td>
<td>
<asp:DropDownList ID="ddlMonth" runat="server" AutoPostBack="true" Style="width: 90px;"
OnSelectedIndexChanged="ddlMonth_SelectedIndexChanged">
<asp:ListItem Selected="True" Value="Select">Select</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
<label>Select Day:</label>
</td>
<td>
<asp:DropDownList ID="ddlDay" runat="server" Style="width: 90px;">
<asp:ListItem Selected="True" Value="Select">Select</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
</table>
</form>
</body>
</html>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadYears();
}
}
private void LoadYears()
{
ddlYear.Items.Clear();
ddlYear.DataSource = Enumerable.Range(2011, 10);
ddlYear.DataBind();
ddlYear.Items.Insert(0, new ListItem("Select", "Select"));
ddlYear.SelectedIndex = 0;
}
protected void ddlMonth_SelectedIndexChanged(object sender, EventArgs e)
{
LoadDays();
}
protected void ddlYear_SelectedIndexChanged(object sender, EventArgs e)
{
LoadMonths();
LoadDays();
}
private void LoadDays()
{
ddlDay.Items.Clear();
if (ddlMonth.SelectedIndex != 0)
{
System.Globalization.DateTimeFormatInfo dtfi =
System.Globalization.DateTimeFormatInfo.CurrentInfo;
int numberOfDaysInMonth =
DateTime.DaysInMonth(Convert.ToInt32(ddlYear.SelectedValue),
Convert.ToInt32(ddlMonth.SelectedValue));
ddlDay.DataSource = Enumerable.Range(1, numberOfDaysInMonth);
ddlDay.DataBind();
}
ddlDay.Items.Insert(0, new ListItem("Select", "Select"));
ddlDay.SelectedIndex = 0;
}
private void LoadMonths()
{
ddlMonth.Items.Clear();
if (ddlYear.SelectedIndex != 0)
{
System.Globalization.DateTimeFormatInfo info =
System.Globalization.DateTimeFormatInfo.CurrentInfo;
for (int i = 1; i <= 12; i++)
{
ddlMonth.Items.Add(new ListItem(info.GetMonthName(i), i.ToString()));
}
}
ddlMonth.Items.Insert(0, new ListItem("Select", "Select"));
ddlMonth.SelectedIndex = 0;
}
</script>