在下拉列表中显示数据取决于其他下拉数据

时间:2013-11-30 06:20:24

标签: c# asp.net drop-down-menu dropdownbox monthcalendar

您好, 我有一个dropdown,以下列格式显示年份

 2012-2013
 2013-2014

现在我有另一个显示月份的dropdown,但它取决于财政年度, 例如:假设如果我选择2013-2014年,那么我想以下列格式显示dropdown中的月份

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

这是因为我们将年度视为财政年度,其中几个月从2013年4月开始到2014年3月结束2013-2014

1 个答案:

答案 0 :(得分:1)

第1步:获取年度SelectedItem中的DropDownList
第2步:使用Split()函数从Selected字符串中获取年份值 第3步:将选定年份的第一年添加到4月到12月的DropDownList月份。
Step4:将第二个选定年份添加到从1月到3月的DropDownList月份。

试试这个:

设计代码:

<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">

代码背后:

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]);
        }

    }
相关问题