Loading ...

Get datetime out in 3 dropdownlists (day, month, year) | CodeAsp.Net

Get datetime out in 3 dropdownlists (day, month, year)

 /5
0 (0votes)

Today I have read a question on forums asp.net. So I thought I should write a blog on this.

Question - How do I get Date, month, year in three different dropdownlist?

Well below you will see you how we can show Date, Month and Year in three different dropdownlist.

Code -

 

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="ddlDate" runat="server" ></asp:DropDownList> 
        <asp:DropDownList ID="ddlMonth" runat="server" ></asp:DropDownList> 
        <asp:DropDownList ID="ddlYear" runat="server" ></asp:DropDownList>
    </div>
    </form>
</body>
</html>

 

using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;

namespace WebProjects
{
    public partial class ddlDayMonthYearSeprate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BindDDL("Year");
            BindDDL("Month");
            BindDDL("Date");
            
        }
        private void BindDDL(string ddl)
        {
            switch (ddl)
            {
                case "Year":
                    ddlYear.DataSource = GetYear();
                    ddlYear.DataBind();
                    ddlYear.Items.Insert(0,new ListItem("Year"));
                    break;
                case "Date":
                    ddlDate.DataSource = GetDate();
                    ddlDate.DataBind();
                    ddlDate.Items.Insert(0, new ListItem("Date"));
                    break;
                case "Month":
                    ddlMonth.DataSource = GetMonth();
                    ddlMonth.DataBind();
                    ddlMonth.Items.Insert(0, new ListItem("Month"));
                    break;
            }
        }
        private List<int> GetYear()
        {
            List<int> year = new List<int>();
            for(int i=(DateTime.Now.Year)-20;i!=(DateTime.Now.Year)+20;i++)
            {
                year.Add(i);
            }
            return year;

        }
        private List<int> GetDate()
        {
            List<int> date = new List<int>();
            for (int i = 1; i <= 31; i++)
            {
                date.Add(i);
            }
            return date;
        }
        private List<int> GetMonth()
        {
            List<int> month = new List<int>();
            for (int i = 1; i <= 12; i++)
            {
                month.Add(i);
            }
            return month;
        }
    }
}

 


In above example there are three dropdownlist which I am binding from list. In year method, I am showing only 20 years previous and 20 years next for current year.

 

Do let me know your feedback or comment.

Comments (2)

   
hajan
Interesting.Have you been thinking about Validation? For example, what if you submit the following valuesay: 31; Month: 2; Year: 2010? It's not a valid date -Anyway, nice code example!
10/22/2010
 · 
by
   
mohit
Thank you Hajan for the comment.Well I didn't think about Validation....but yes I will work on it..
10/22/2010
 · 
by
  • :*
  • :*
  • :
 *

Top Posts