if下拉列表项的语句

时间:2016-04-15 15:22:03

标签: html asp.net dropdown listitem

我希望有一个送货地址功能,如果我选择一个特定的城市,它将在下一个下拉列表项目中显示不同的选项。这是我的HTML代码:

<td>
        City
    </td>
    <td>
        <asp:DropDownList ID="DropDownList1" runat="server" placeholder="Income Range...">
                                <asp:ListItem Value="item1" Selected="True">--Select--</asp:ListItem>
                                <asp:ListItem Value="item2" Selected="True">--Las Pinas--</asp:ListItem>
                                <asp:ListItem Value="item3" Selected="True">--Muntinlupa--</asp:ListItem>
                                <asp:ListItem Value="item4" Selected="True">--Paranaque--</asp:ListItem>
                                <asp:ListItem Value="item5" Selected="True">--Pasay--</asp:ListItem>
                                </asp:DropDownList>
    </td>

例如,我选择item1,它将显示不同的目的地。如果是item2,它将显示另一组目的地,依此类推。你可能与我分享的任何伎俩?谢谢

我尝试了这个代码:

if (DropDownList1.SelectedItem.Text.Equals("Las Pinas"))
        {
            DropDownListCity2.Items.Add("Almanza");
            DropDownListCity2.Items.Add("Almanza Dos");
        }

但它什么也没做。请帮帮我

1 个答案:

答案 0 :(得分:0)

虽然想通了。

在下拉列表中设置autopostback =“true”。

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true">
                                <asp:ListItem Value="item1" Selected="True">--Select--</asp:ListItem>
                                <asp:ListItem Value="item2" >Las Pinas</asp:ListItem>
                                <asp:ListItem Value="item3" >Muntinlupa</asp:ListItem>
                                <asp:ListItem Value="item4" >Paranaque</asp:ListItem>
                                <asp:ListItem Value="item5" >Pasay</asp:ListItem>
                                </asp:DropDownList>

<td>
        <asp:DropDownList ID="DropDownListCity2" runat="server" >

                                </asp:DropDownList>
    </td>

然后把它放在后面的代码上:

if (DropDownList1.SelectedItem.Text.Equals("Las Pinas"))
        {
            DropDownListCity2.Items.Add("Almanza");
            DropDownListCity2.Items.Add("Almanza Dos");
        }
相关问题