locked
Select Day, Month and Year Date from DropDownList in C# RRS feed

  • Question

  • User1151703306 posted

    Hi all, I need your help.

    I use this code for populate my own Day, Month and Year Date Selector using three DropDownList in ASP.Net

    Now I need set as default value for each DDL the vakue of DateTime.MinValue, that is 01/01/0001 , because the value of date to be recorded in the database is not always available :

    1. in DDL ddlday I need as default value : 01;
    2. in DDL ddlMonth I need as default value : January;
    3. in DDL ddlYear I need as default value : 0001;

    Using this code I don't have error but :

    1. in DDL ddlday I have as default value : 01;
    2. in DDL ddlMonth I have as default value : january;
    3. in DDL ddlYear I have as default value : 2019;

    Please can you help me ?

    My code below.

    code-behind

    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {           
                DateTime dateofBirth = DateTime.MinValue;
    
                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.SelectedValue = dateofBirth.Day.ToString();
                ddlMonth.SelectedValue = dateofBirth.Month.ToString();
                ddlYear.SelectedValue = dateofBirth.Year.ToString();
    
                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();
        }
    }

    Markup

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:ScriptManager ID="sc" runat="server">
                </asp:ScriptManager>
                <table>
                    <tr>
                        <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: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:DropDownList ID="ddlYear" runat="server">
                            </asp:DropDownList>
                        </td>
                    </tr>
                </table>
            </div>
        </form>
    </body>
    </html>

    Saturday, October 5, 2019 4:05 PM

Answers

  • User475983607 posted

    I need include min year in the range equals to 0001 and add interval 2019-2030.

    When the values selected in DDL are 01/January/0001 means that a date is not available for recorded in the database

    My best guess is you want to insert 0001 to the ddlYear dropdown.

    ddlYear.Items.Insert(0, new ListItem("0001", "0001"));

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, October 5, 2019 4:38 PM

All replies

  • User475983607 posted

    You coded the dropdown to contains a list for the last 100 years not 2019 years.

    ddlYear.DataSource = Enumerable.Range(DateTime.Now.Year - 99, 100).Reverse();

    Saturday, October 5, 2019 4:26 PM
  • User1151703306 posted

    mgebhard

    You coded the dropdown to contains a list for the last 100 years not 2019 years.

    ddlYear.DataSource = Enumerable.Range(DateTime.Now.Year - 99, 100).Reverse();

    Hi mgebhard, thank you for reply.

    I need include min year in the range equals to 0001 and add interval 2019-2030.

    When the values selected in DDL are 01/January/0001 means that a date is not available for recorded in the database.

    ddlYear.DataSource = Enumerable.Range(DateTime.Now.Year - 0, 12).Reverse();



    Saturday, October 5, 2019 4:28 PM
  • User475983607 posted

    I need include min year in the range equals to 0001 and add interval 2019-2030.

    When the values selected in DDL are 01/January/0001 means that a date is not available for recorded in the database

    My best guess is you want to insert 0001 to the ddlYear dropdown.

    ddlYear.Items.Insert(0, new ListItem("0001", "0001"));

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, October 5, 2019 4:38 PM
  • User1151703306 posted

    mgebhard

    Golia

    I need include min year in the range equals to 0001 and add interval 2019-2030.

    When the values selected in DDL are 01/January/0001 means that a date is not available for recorded in the database

    My best guess is you want to insert 0001 to the ddlYear dropdown.

    ddlYear.Items.Insert(0, new ListItem("0001", "0001"));

                ddlYear.DataSource = Enumerable.Range(DateTime.Now.Year - 0, 12).Reverse();            
                ddlYear.DataBind();
    ddlYear.Items.Insert(0, new ListItem("0001", "0001"));

    In the ddl Year the value "0001" is now available, thank you!

    Saturday, October 5, 2019 4:46 PM
  • User475983607 posted

    Data bind then add the item.

    ddlYear.DataSource = Enumerable.Range(DateTime.Now.Year - 0, 12).Reverse();
    ddlYear.DataBind();
    ddlYear.Items.Insert(0, new ListItem("0001", "0001"));

    Saturday, October 5, 2019 5:02 PM