to display data in dropdown depends on other dropdown data

1k views Asked by At

Hi, i have one dropdown which display year in following format

 2012-2013
 2013-2014

now i have another dropdown which displays month but its depend on financial year, for eg:suppose if i select year 2013-2014 then i want to display month in dropdown in following format

April 2013 
march 2013
may 2013
june 2013
july 2013
august 2013
sept 2013
oct 2013
november 2013
december 2013
jan 2014
feb 2014
march 2014

this is because we consider year as financial year in which months start from april 2013 and end on march 2014 for year 2013-2014

1

There are 1 answers

0
Sudhakar Tillapudi On BEST ANSWER

Step1: get the SelectedItem from the Year DropDownList .
Step2: use Split() function to get the year values from the Selected String
Step3: Append first year in selected Year to the months DropDownList from April to December.
Step4: Append Second Selected Year to the months DropDownList from january to March.

Try This:

Design Code:

<asp:DropDownList ID="drpYear" runat="server" AutoPostBack="True" OnSelectedIndexChanged="drpYear_SelectedIndexChanged">
        <asp:ListItem>2012-2013</asp:ListItem>
        <asp:ListItem>2013-2014</asp:ListItem>
    </asp:DropDownList>
    <asp:DropDownList ID="drpMonth" runat="server">

Code Behind:

protected void drpYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        String [] Months =new String[] { "April", "May", "June", "July", "August", "September", "October", "November", "December", "January", "February", "March"};
        drpMonth.Items.Clear();
        for (int i = 0; i < Months.Length; i++)
        {
            if(i<9)
            drpMonth.Items.Add(Months[i]+" "+drpYear.SelectedItem.ToString().Split('-')[0]);
            else
                drpMonth.Items.Add(Months[i] + " " + drpYear.SelectedItem.ToString().Split('-')[1]);
        }

    }