Monday 8 April 2013

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

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


Open New WebSite and Add a Form Naming it asDDLYearsAndMonth
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();
      
      
       
       
        }
   
   
   
    }

1 comment:

  1. Bloggers often use their platforms to advocate for social, environmental, and political issues, mobilizing communities for positive change. Virtualization Utilities Industry Objective reporting in articles presents facts without bias, providing readers with a balanced perspective on news and events.

    ReplyDelete