I have two list boxes that I am trying to pass multiple values from one to the other. asp.net code
<asp:UpdatePanel ID="userButtonPanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:LinkButton Text=">>" ID="lbtn_add" CssClass="button advButtons addRemButton"
runat="server" OnClick="addMappedUser_Click" />
<asp:LinkButton Text="<<" ID="lbtn_remove" CssClass="button advButtons addRemButton"
runat="server" OnClick="removeMappedUser_Click" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
<div>
<asp:Label ID="lbl_MappedUSers" Text="Mapped Users" runat="server" />
<asp:ListBox ID="listMappedUsers" runat="server" SelectionMode="Multiple" CssClass="listboxMappedUsers" />
</div>
</div>
</div>
<input id="cbUserList" type="hidden" runat="server" value="" />
<input id="cbUserRoleID" type="hidden" runat="server" value="" />
</ContentTemplate>
</asp:UpdatePanel>
On the click event of addMappedUser_Click I evaluate the listAllUsers listbox to see what items are selected and create a list of these items
protected void addMappedUser_Click(object sender, EventArgs e)
{
List<ListItem> selectedItems = listAllUsers.Items.Cast<ListItem>().Where(i => i.Selected).ToList();
List<AddedUsers> newusers = new List<AddedUsers>();
foreach (var item in selectedItems)
{
AddedUsers au = new AddedUsers();
au.UserID = item.Value;
au.UserName = item.Text;
newusers.Add(au);
}
MoveSelectedItems(newusers, listAllUsers);
}
Once that list is create it is then passed to the MoveSelectedItems Method
protected void MoveSelectedItems(List<AddedUsers> from, ListBox to)
{
if (from.Count == 0)
{
return;
}
SecurityUserRole sur = new SecurityUserRole();
ListItem item = new ListItem();
foreach (var i in from)
{
item.Value = i.UserID;
item.Text = i.UserName;
if (listAllUsers.Items.Contains(item))
{
listAllUsers.Items.Remove(item);
listMappedUsers.Items.Add(item);
}
sur.SecurityUserId = Convert.ToInt32(i.UserID);
sur.SecurityRoleId = Convert.ToInt32(RoleID);
MappedUser.Add(sur);
Session["MappedUser"] = MappedUser;
checkMappingButtons(listMappedUsers, listAllUsers);
editUserUpdatePanel.Update();
}
}
Where I am getting stuck is in the adding of the users to the second listbox (listMappedUsers.Items.Add) When the I set a breakpoint I can see each value pass through (for example "201", "202", "203") however when the updatepanel updates the listbox will only have 203 listed three times.
Can someone help me understand what I am missing and why the last value is the only value appearing in the list box vs it's previous predecessors?
Thanks in advance,