Adding Date ,Year and Month in DropDownlist using ASP.NET,C#

 

Open New WebSite and Add a Form Naming it as DDLYearsAndMonth

ASPX  Page 

Copy and paste this code between  DIV  opening and closing tags

 

<fieldset>

    <legend>DropDownListBindingDate</legend>

        <asp:DropDownList ID=”DropDownList1″ runat=”server” OnSelectedIndexChanged=”DropDownList1_SelectedIndexChanged” AutoPostBack=”True”>

        </asp:DropDownList>

        <asp:DropDownList ID=”DropDownList2″ runat=”server” OnSelectedIndexChanged=”DropDownList2_SelectedIndexChanged” AutoPostBack=”True”>

        </asp:DropDownList>

        <asp:DropDownList ID=”DropDownList3″ runat=”server” AutoPostBack=”True”>

        </asp:DropDownList><br />

        <asp:Label ID=”Label1″ runat=”server”></asp:Label><br />

       </fieldset>

 

 

ASPX.CS  Page

 

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

public partial class DDLYearsAndMonth : System.Web.UI.Page

{

    int year, month;

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

            DateTime tnow = DateTime.Now;

 

            ArrayList ayear = new ArrayList();

            int i;

            for (i = 1980; i <= 2008; i++)

            {

 

                ayear.Add(i);

 

            }

 

 

            ArrayList amonth = new ArrayList();

            for (i = 1; i <= 12; i++)

            {

 

                amonth.Add(i);

 

            }

 

 

            DropDownList1.DataSource = ayear;

            DropDownList1.DataBind();

 

            DropDownList2.DataSource = amonth;

            DropDownList2.DataBind();

 

            DropDownList1.SelectedValue = tnow.Year.ToString();

            DropDownList2.SelectedValue = tnow.Month.ToString();

 

            year = Int32.Parse(DropDownList1.SelectedValue);

            month = Int32.Parse(DropDownList2.SelectedValue);

 

            BindDays(year, month);

            DropDownList3.SelectedValue = tnow.Day.ToString();

        }

 

    }

 

    private bool CheckLeapYear(int year)

    {

        if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))

            return true;

        else return false;

   

    }

 

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)

    {

          year = Int32.Parse(DropDownList1.SelectedValue);

        month = Int32.Parse(DropDownList2.SelectedValue);

        BindDays(year, month);

    }

    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)

    {

          year = Int32.Parse(DropDownList1.SelectedValue);

        month = Int32.Parse(DropDownList2.SelectedValue);

        BindDays(year, month);

    }

 

    private void BindDays(int year, int month)

    {

        int i;

        ArrayList aday = new ArrayList();

 

        switch (month)

        {

            case 1:

            case 3:

            case 5:

            case 7:

            case 8:

            case 10:

            case 12:

                for (i = 1; i <= 31; i++)

                {

                    aday.Add(i);

               

                }

                break;

            case 2:

 

                if (CheckLeapYear(year))

                {

 

                    for (i = 1; i <= 29; i++)

                 

 

                        aday.Add(i);

                }

                 

                    else

                    {

                   

                    for(i=1;i<=28;i++)

                  

                         aday.Add(i);

                   

                  

                    }

 

                 break;

            case 4:

            case 6:

            case 9:

            case 11:

                for(i=1;i<=30;i++)

                    aday.Add(i);

                break;

 

 

                }

 

        DropDownList3.DataSource=aday;

        DropDownList3.DataBind();

      

      

       

       

        }

   

   

   

    }

One Response to “Adding Date ,Year and Month in DropDownlist using ASP.NET,C#”

  1. Maria Says:

    Thank u very much,it helped me sooo much!! :)

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