ASP:ListBox - 回发中没有选定的项目?

Kei*_*ows 3 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>
Run Code Online (Sandbox Code Playgroud)

代码隐藏中的以下内容:

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();
}
Run Code Online (Sandbox Code Playgroud)

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

Ric*_*dOD 7

你是否availableRolesListBox每次重新绑定,而不是if(!IsPostback)?