How to fill day, month and year dropdownlist asp.net and c#?

Saroj13
Saroj13 used Ask the Experts™
on
Hi ,

I have this code to fill the Month, Day and Year dropdownlist in asp.net and c#. How to fill day dropdown so that it will have appropriate days in the dropdown for selected month and year.
Currently day dropdown is not having the appropriate days for the selected month and year

 DateTime month = Convert.ToDateTime("1/1/2000");
            for (int i = 0; i < 12; i++)
            {
                DateTime NextMont = month.AddMonths(i);
                ListItem list = new ListItem();
                list.Text = NextMont.ToString("MMMM");
                list.Value = NextMont.Month.ToString();
                this.ddlMonth.Items.Add(list);
            }
            ddlMonth.Items.FindByValue((DateTime.Now.Month).ToString()).Selected = true;

       
         //   ddlDate.Items.Add(new ListItem("Day", "Day"));
         //   ddlDate.SelectedIndex = 0;

            int totalDays = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
            for (int intLoop = 1; intLoop <= totalDays; intLoop++)
            {
                ddlDay.Items.Add(new ListItem(intLoop.ToString(), intLoop.ToString()));
            }

            ddlDay.Items.FindByValue((DateTime.Now.Day).ToString()).Selected = true;

          //  ddlYear.Items.Add(new ListItem("Year", "Year"));
         //   ddlYear.SelectedIndex = 0;

            int intYearName = 2008;
            for (int intLoop = intYearName; intLoop <= Convert.ToInt32(DateTime.Now.Year); intLoop++)
            {
                ddlYear.Items.Add(new ListItem(intLoop.ToString(), intLoop.ToString()));
            }

            ddlYear.Items.FindByValue((DateTime.Now.Year).ToString()).Selected = true;
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2011

Commented:
see solution
asp .net day month dropdown list box creation
http://forums.asp.net/t/1134942.aspx/1

Author

Commented:
Its written in vb.net. i need something in c#
Top Expert 2011
Commented:
translated

 protected void Page_Load(object sender, System.EventArgs e) {
        if (!IsPostBack) {
            //  Initialise month
            for (int month = 1; (month <= 12); month++) {
                ddlMonth.Items.Add(new ListItem(month.ToString(), month.ToString()));
            }
            //  Set to current month
            ddlMonth.SelectedValue = DateTime.Now.Month.ToString();
            // Initialise day with DaysInMonth on selected month
            for (int day = 1; (day <= DateTime.DaysInMonth(DateTime.Now.Year, Convert.ToInt32(ddlMonth.SelectedItem.Value))); day++) {
                ddlDay.Items.Add(new ListItem(day.ToString(), day.ToString()));
            }
            //  Set to current day
            ddlDay.SelectedValue = DateTime.Now.Day.ToString();
        }
    }
   
    protected void ddlMonth_SelectedIndexChanged(object sender, System.EventArgs e) {
        // Reset previous
        ddlDay.Items.Clear();
        // Initialise day with DaysInMonth on selected month
        for (int day = 1; (day <= DateTime.DaysInMonth(DateTime.Now.Year, Convert.ToInt32(ddlMonth.SelectedItem.Value))); day++) {
            ddlDay.Items.Add(new ListItem(day.ToString(), day.ToString()));
        }
    }

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial