asp 下拉列表多项选择

asp Dropdownlist multiple item selection

我正在尝试从下拉列表中 select 多个项目,我正在使用 bootstrap,但我的问题是: 我只能得到第一个 selected 项目,不能得到其他 selected 项目。

我的代码(样式和脚本):

 <link rel="stylesheet" href="Content/bootstrap.min.css" /> <!-- Bootstrap v4.4.1 -->
 <link rel="stylesheet" href="Content/bootstrap-select.css" /> <!-- Bootstrap-select v1.13.1 -->
 <script src="js/jquery-1.9.1.js"></script>
 <script src="Scripts/bootstrap.bundle.min.js"></script>
 <script src="Scripts/bootstrap-select.min.js"></script> <!-- Bootstrap-select v1.13.1  -->
 <script type="text/javascript">
    $('select').selectpicker();
 </script>

我的 DDL:

<asp:DropDownList CssClass="selectpicker" ID="DropDownList1" runat="server" multiple data-live-search="true"  SelectionMode="multiple">
            <asp:ListItem>Brasil</asp:ListItem>
            <asp:ListItem>Colombia</asp:ListItem>
            <asp:ListItem>United States</asp:ListItem>
            <asp:ListItem>Frannce</asp:ListItem>
            <asp:ListItem>Italy</asp:ListItem>
            <asp:ListItem>Japan</asp:ListItem>
        </asp:DropDownList>

我使用一个按钮,这是 OnClick 命令:

protected void btnSend_Click(object sender, EventArgs e)
{
    string selectedItems = "";
    foreach (ListItem item in DropDownList1.Items)
    {
        if (item.Selected)
        {
            selectedItems += item.Text + "\n";
        }
    }
    ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('" + selectedItems + "');", true);
}

正如我所说..我只能得到第一个项目,请任何人帮助我? 谢谢

此致

DropDownList

allows the user to select a single item from a drop-down list

一个 ListBox 允许您 select 多个项目:

<asp:ListBox CssClass="selectpicker" 
             ID="DropDownList1" 
             runat="server" 
             data-live-search="true"  
             SelectionMode="Multiple">
    <asp:ListItem>Brasil</asp:ListItem>
    <asp:ListItem>Colombia</asp:ListItem>
    <asp:ListItem>United States</asp:ListItem>
    <asp:ListItem>Frannce</asp:ListItem>
    <asp:ListItem>Italy</asp:ListItem>
    <asp:ListItem>Japan</asp:ListItem>
</asp:ListBox>

您的其余代码应该可以正常工作,也许可以更新 ID。 Aspsnippets 有一个使用 bootstrap 的例子。