如何在 ASP 中制作自动回发多选列表框?

How to make autopostback Multiselect ListBox in ASP?

我正在尝试使用具有 Bootstrap Multiselect 样式的 ListBox 控件在 ASP.NET 中制作一个多 select ListBox。

外观方面,做工不错。我可以将其切换为下拉,我可以 select 和 deselect 选项并关闭它。

<asp:ListBox ID="NameListBox" runat="server" CssClass="multiselect" SelectionMode="Multiple" OnSelectedIndexChanged="NameListBox_SelectedIndexChanged">
    <asp:ListItem Text="Name 1" />
    <asp:ListItem Text="Name 2" />
    <asp:ListItem Text="Name 3" />
    <asp:ListItem Text="Name 4" />
    <asp:ListItem Text="Name 5" />
    <asp:ListItem Text="Name 6" />
</asp:ListBox>

我遇到的问题是进行回发和处理新 selected 的项目。我写了一个 OnSelectedIndexChanged,我在其中循环遍历 selected 项目并将它们插入数据库,但我不知道何时以及如何 运行 它。我知道我可以有一个单独的按钮来引起回发并保存 selected 项目,但我想在关闭 ListBox 时引起自动回发。

问题是,如果我使用 AutoPostBack="true",那么每次选项更改都会发生回发,因此用户无法 select 除了一次保存之外的更多选项。我只想在下拉列表实际关闭时引起回发。

经过多次尝试,我得到了这个:

$('.multiselect').multiselect({
            nonSelectedText: '',
            nSelectedText: 'selected',
            allSelectedText: 'Everyone',
            numberDisplayed: 2,
            buttonTextAlignment: 'left',
            buttonWidth: 240,
            onDropdownHidden: function (event) {
                __doPostBack($(event.target).parent().children('.multiselect').attr('id'), '');
            }
            });

这会导致在关闭列表框的下拉列表后回发,这很好。但遗憾的是,NameListBox_SelectedIndexChanged 事件处理程序在回发期间没有被触发,所以什么也没有发生。

有什么建议吗?

希望对您有所帮助~

<asp:ListBox ID="DropDownList_ExportCountry" runat="server" SelectionMode="Multiple" OnSelectedIndexChanged="DropDownList_ExportCountry_SelectedIndexChanged"></asp:ListBox>

在 html

<script type="text/javascript">
$('[id*=DropDownList_ExportCountry]').multiselect({
        onDropdownHide: function (event) {
            __doPostBack();//__doPostBack($(event.target).parent().children('#DropDownList_ExportCountry').attr('id'), '')
        }
    });

并记住在 中像这样包含 js 和 Css

<link href="http://cdn.rawgit.com/davidstutz/bootstrap-multiselect/master/dist/css/bootstrap-multiselect.css" rel="stylesheet" type="text/css" />
<script src="http://cdn.rawgit.com/davidstutz/bootstrap-multiselect/master/dist/js/bootstrap-multiselect.js" type="text/javascript"></script>