asp.net - ASP :ListBox - no selected items on postback?

标签 asp.net listbox selecteditem

我有以下标记:

<tr>
    <td valign="top" align="left">
        <asp:Label ID="Label1" runat="server" Text="Available Roles" />
        <br />
        <asp:ListBox ID="availableRolesListBox" runat="server" SelectionMode="Multiple" Width="100px" Rows="10" AutoPostBack="false" />
    </td>
    <td valign="top" align="center">
        &nbsp;
        <br />
        <asp:Button ID="addToRole" runat="server" Text="--->" OnClick="addToRole_Click" />
        <br />
        <asp:Button ID="removeFromRole" runat="server" Text="<---" OnClick="removeFromRole_Click" />
    </td>
    <td valign="top" align="left">
        <asp:Label ID="Label2" runat="server" Text="User In Roles" />
        <br />
        <asp:ListBox ID="userInRolesListBox" runat="server" SelectionMode="Multiple" Width="100px" Rows="10" AutoPostBack="false" />
    </td>
</tr>

以及代码隐藏中的以下内容:

protected void addToRole_Click(object sender, EventArgs e)
{
    // Add user to the selected role...
    foreach (ListItem myItem in availableRolesListBox.Items)
    {
        if (myItem.Selected)
        {
            Roles.AddUserToRole(userListBox.SelectedItem.Value, myItem.Text);
        }
    }

    Refresh();
}

当我进入代码隐藏时,绝对没有选择任何项目!我忘记了什么?

最佳答案

您是否可能每次都重新绑定(bind) availableRolesListBox,而不是 if(!IsPostback)?

关于asp.net - ASP :ListBox - no selected items on postback?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1255475/

相关文章:

c# - ASP.NET:多台服务器上的 SHA1 + Salt 密码哈希

c# - 将日期时间转换为格式 "dd.MM.yyyy"从另一个时区到 +2

asp.net - 使用 Ninject.MVC3 在测试项目中添加绑定(bind)

excel - Excel 下拉框是否可以像带有用于多项选择的复选框的列表框一样?

asp.net-mvc - 在 asp.net mvc 选择列表上获取选定的值

MVVM DataGrid 从选定单元格复制信息

c# - ASP.NET Core 2.0 中的防伪 cookie

c# - 在 C# 中处理 ListBox 项上的双击事件

.net - 如何在 WPF 中使列表框透明,但列表框项不透明?

c# - 如何获取 SelectedItem 的所有子项