Tuesday, 4 December 2012

Select DOB from dropdownlist Year,Month,Date

Select DOB from dropdownlist Year,Month,Date


<asp:UpdatePanel ID="updatepanel2" runat="server">
        <ContentTemplate>
        <asp:DropDownList ID="ddlBirthyear" runat="server" AutoPostBack="True" ToolTip="Select Year" TabIndex="13"
                                            onselectedindexchanged="ddlBirthyear_SelectedIndexChanged">
                                              <asp:ListItem Text="-- Year --" Value=""> </asp:ListItem>
                                        </asp:DropDownList>
                                        <asp:DropDownList ID="ddlBirthmonth" runat="server" AutoPostBack="True" ToolTip="Select Month" TabIndex="14"
                                            onselectedindexchanged="ddlBirthmonth_SelectedIndexChanged">
                                              <asp:ListItem Text="-- Month --" Value=""> </asp:ListItem>
                                        </asp:DropDownList>
                                         <asp:DropDownList ID="ddlBirthdate" runat="server" ToolTip="Select Date" TabIndex="15" >
                                          <asp:ListItem Text="-- Date --" Value=""> </asp:ListItem>
                                        </asp:DropDownList>
        </ContentTemplate>
        </asp:UpdatePanel>

.cs code

 protected void Page_Load(object sender, EventArgs e)
    {
        #region DOB DDL

        for (int i = 1; i <= 31; i++)
        {
            ddlBirthdate.Items.Add(i.ToString());
        }

        int[] n = (int[])Enum.GetValues(typeof(Month));

        foreach (int x in n)
        {
            ddlBirthmonth.Items.Add(new ListItem(Enum.Parse(typeof(Month), x.ToString()).ToString(), x.ToString()));
        }


        //for (int i = 1; i <= 12; i++)
        //{

        //    ddlBirthmonth.Items.Add(i.ToString());
        //}
        for (int i = DateTime.Now.Year; i >= 1960; i--)
        {
            ddlBirthyear.Items.Add(i.ToString());
        }

        #endregion
    }

    #region  DDL DOB - Binds Drop down for date of birth

    public enum Month
    {
        January = 1,
        February = 2,
        March = 3,
        April = 4,
        May = 5,
        June = 6,
        July = 7,
        August = 8,
        September = 9,
        October = 10,
        November = 11,
        December = 12
    }

    private bool CheckLeap(int year)
    {
        if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
            return true;
        else
            return false;
    }

    private void BindDays(int year, int month)
    {
        int i;
        System.Collections.ArrayList AlDay = new System.Collections.ArrayList();

        switch (month)
        {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                for (i = 1; i <= 31; i++)
                    AlDay.Add(i);
                break;
            case 2:
                if (CheckLeap(year))
                {
                    for (i = 1; i <= 29; i++)
                        AlDay.Add(i);
                }
                else
                {
                    for (i = 1; i <= 28; i++)
                        AlDay.Add(i);
                }
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                for (i = 1; i <= 30; i++)
                    AlDay.Add(i);
                break;
        }

        ddlBirthdate.DataSource = AlDay;
        ddlBirthdate.DataBind();
        ddlBirthdate.Items.Insert(0, new ListItem("-- Date --"));

    }

    protected void ddlBirthyear_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlBirthyear.SelectedIndex != 0)
        {
            int year = Int32.Parse(ddlBirthyear.SelectedValue);
            int month = 1;
            try { month = Int32.Parse(ddlBirthmonth.SelectedValue); }
            catch { }
            BindDays(year, month);
        }
        else
        {
            ddlBirthdate.SelectedIndex = 0;
        }

    }

    protected void ddlBirthmonth_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlBirthmonth.SelectedIndex != 0)
        {
            int year = 2000;
            try { year = Int32.Parse(ddlBirthyear.SelectedValue); }
            catch { }
            int month = Int32.Parse(ddlBirthmonth.SelectedValue);
            BindDays(year, month);
        }
        else
        {
            ddlBirthdate.SelectedIndex = 0;

        }
    }

    #endregion
  
    protected void btnsave_Click(object sender, EventArgs e)
    {
        String DOB = ddlBirthdate.SelectedItem.Text + "/" + ddlBirthmonth.SelectedValue + "/" + ddlBirthyear.SelectedValue;
    }
}

No comments:

Post a Comment