Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8967219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:11:37+00:00 2026-06-15T17:11:37+00:00

I have two list boxes that I am trying to pass multiple values from

  • 0

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,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:11:38+00:00Added an answer on June 15, 2026 at 5:11 pm

    In your last section of code, you will need to instantiate a new ListItem in your foreach. As it is now, you are editting the same item over and over in the foreach.

    protected void MoveSelectedItems(List<AddedUsers> from, ListBox to)
        {
    
            if (from.Count == 0)
            {
                return;
            }
    
            SecurityUserRole sur = new SecurityUserRole();
    
            ListItem item;
            foreach (var i in from)
            {
                item = new ListItem();
                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();
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two listboxes that I am dragging one item from to the other.
I have two WPF list boxes. One is a list of lists (actually a
I've got two list boxes, one a master, the other a child. When the
I have two list boxes and some JQuery function that does the following: $(document).ready(function
I have a form that needs to use two combo boxes but the values
I have two list boxes with one to one mapping. They are also scrollable.
I have two select boxes. One with list of Cities and the second one
I have a view that contains 2 list boxes: Audience & Locale I'm trying
I have a code that fetches a list from DB. Using that returned array
I have two text input boxes that will need to be populated from the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.