根据asp.net中的多选状态填写城市下拉列表

时间:2016-12-28 07:47:53

标签: c# asp.net multi-select

我正在创建一个表单,用户可以从下拉列表中选择国家/地区,然后根据所选国家/地区,在多选列表框中填写所有相关状态。现在,用户可以选择多个状态,并且基于所选状态,应相应地填充另一个多选列表框(为城市分配)。怎么能实现这个目标?

enter image description here

HTML: -

<asp:DropDownList ID="ddlCountry" runat="server" CssClass="form-control" AutoPostBack="true OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged">/asp:DropDownList>

<asp:ListBox ID="ddlState" runat="server" SelectionMode="Multiple" CssClass="form-control multiselectmulticolumnddl" OnSelectedIndexChanged="ddlState_SelectedIndexChanged"></asp:ListBox>

<asp:ListBox ID="ddlCity" runat="server" SelectionMode="Multiple" CssClass="form-control multiselectmulticolumnddl"></asp:ListBox>

2 个答案:

答案 0 :(得分:0)

对于ListBox,您需要使用SelectedIndexChanged Event

<asp:ListBox ID="ddlState" runat="server" SelectionMode="Multiple" OnSelectedIndexChanged="ddlState_SelectedIndexChanged" CssClass="form-control multiselectmulticolumnddl"></asp:ListBox>

您还需要使用Ajax进行部分发布。

答案 1 :(得分:0)

据我所知,你已经有了一份州名单。因为你在ddlCountry的火上选择了索引。

protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
    {
    // Make a stateList where we store all stateIDs. 
    List<int> stateList = new List<int>();

    // Loop through all items and check if they are selected. If yes then we add them to our list of selected items.
    foreach (ListItem state in ddlState.Items)
    {
     if(state.selected){
     {
    //we add the ID of the state. 
     stateList.Add(state.ID)
     }
    }
 // get all city's assosiated with the states in the stateList
}

这将为您提供stateID列表。使用此列表填写城市。

希望这就是你要找的。

编辑:确保您为此列表框提供自己的索引更改事件

<asp:ListBox ID="ddlState" runat="server" SelectionMode="Multiple" CssClass="form-control multiselectmulticolumnddl" OnSelectedIndexChanged="ddlState_SelectedIndexChanged"></asp:ListBox>