Home > ASP.Net > Month year day DropDownList for Date Selection in ASP.NET

Month year day DropDownList for Date Selection in ASP.NET


Hi all,

try this example to display Month year day DropDownList for Date Selection in ASP.NET


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
 <title></title>
</head>
<body>
 <form id="form1" runat="server">
 <asp:ScriptManager ID="sc" runat="server">
 </asp:ScriptManager>
 <table>
 <tr>
 <td>
 <asp:UpdatePanel ID="updpnlMonth" runat="server" ChildrenAsTriggers="false" UpdateMode="Conditional">
 <ContentTemplate>
 <asp:DropDownList ID="ddlMonth" runat="server" AutoPostBack="true" DataTextField="MonthName"
 DataValueField="MonthNumber" OnSelectedIndexChanged="ddlMonth_SelectedIndexChanged">
 </asp:DropDownList>
 </ContentTemplate>
 </asp:UpdatePanel>
 </td>
 <td>
 <asp:UpdatePanel ID="updpnlDay" runat="server" UpdateMode="Conditional">
 <ContentTemplate>
 <asp:DropDownList ID="ddlday" runat="server">
 </asp:DropDownList>
 </ContentTemplate>
 <Triggers>
 <asp:AsyncPostBackTrigger ControlID="ddlMonth" EventName="SelectedIndexChanged" />
 </Triggers>
 </asp:UpdatePanel>
 </td>
 <td>
 <asp:DropDownList ID="ddlYear" runat="server">
 </asp:DropDownList>
 </td>
 </tr>
 </table>
 </form>
</body>
</html>

In Code behind :


using System;
using System.Globalization;
using System.Linq;
using System.Web.UI.WebControls;

protected void Page_Load(object sender, EventArgs e)
{
 if (!IsPostBack)
 {
 //Populate DropDownLists
 ddlMonth.DataSource = Enumerable.Range(1, 12).Select(a => new
 {
 MonthName = DateTimeFormatInfo.CurrentInfo.GetMonthName(a),
 MonthNumber = a
 });
 ddlMonth.DataBind();
 ddlYear.DataSource = Enumerable.Range(DateTime.Now.Year - 99, 100).Reverse();
 ddlYear.DataBind();
 ddlday.DataSource = Enumerable.Range(1, DateTime.DaysInMonth(DateTime.Now.Year, Convert.ToInt32(ddlMonth.SelectedValue)));
 ddlday.DataBind();
 }
}

protected void ddlMonth_SelectedIndexChanged(object sender, EventArgs e)
{
 ddlday.DataSource = Enumerable.Range(1, DateTime.DaysInMonth(DateTime.Now.Year, Convert.ToInt32(ddlMonth.SelectedValue)));
 ddlday.DataBind();
}

Hope this helps

Good Luck.

Advertisements
Categories: ASP.Net
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: