Code For Date Of Birth -

How to create DOB selection in asp?

Snippet Code

Rate this page :
  [ 0 votes]

The sample code is used to create a dropdown list box for date of birth. Code behind dropdownlist is written using C

<fieldset style="width:300px"> <legend>Select Date</legend> Year: <asp:DropDownList ID="ddlyear" runat="server" AutoPostBack="True" onselectedindexchanged="ddlyear_SelectedIndexChanged" ></asp:DropDownList> Month: <asp:DropDownList ID="ddlmonth" runat="server" AutoPostBack="True" onselectedindexchanged="ddlmonth_SelectedIndexChanged"> </asp:DropDownList> Day: <asp:DropDownList ID="ddlday" runat="server"> </asp:DropDownList> </fieldset> C#.NET Code to fill dropdownlist with days, months and years in protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { //Fill Years for (int i = 2013; i <= 2020; i ) { ddlyear.Items.Add(i.ToString()); } ddlyear.Items.FindByValue(System.DateTime.Now.Year.ToString()).Selected = true; //set current year as selected //Fill Months for (int i = 1; i <= 12; i ) { ddlmonth.Items.Add(i.ToString()); } ddlmonth.Items.FindByValue(System.DateTime.Now.Month.ToString()).Selected = true; // Set current month as selected //Fill days FillDays(); } } public void FillDays() { ddlday.Items.Clear(); //getting numbner of days in selected month &year int noofdays = DateTime.DaysInMonth(Convert.ToInt32(ddlyear.SelectedValue), Convert.ToInt32(ddlmonth.SelectedValue)); //Fill days for (int i = 1; i <= noofdays; i ) { ddlday.Items.Add(i.ToString()); } ddlday.Items.FindByValue(System.DateTime.Now.Day.ToString()).Selected = true;// Set current date as selected } protected void ddlyear_SelectedIndexChanged(object sender, EventArgs e) { FillDays(); } protected void ddlmonth_SelectedIndexChanged(object sender, EventArgs e) { FillDays(); }


Ask Questions

Ask Question