4

I need to remove the selected items from a ListBox in asp.net. I keep finding examples for windows forms but not for asp.net.

I have a button click event that copies all items from one listbox to another. I want to be able to select individual items from the second listbox and click a button to remove them.

protected void btnAddAllProjects_Click(object sender, EventArgs e)
{

    foreach (ListItem item in lstbxFromUserProjects.Items)
    {
        lstBoxToUserProjects.Items.Add(item.ToString());
    }


}

    protected void btnRemoveSelected_Click(object sender, EventArgs e)
    {}
1
  • How is your data bound to the ListBox? Do you have a List bounded to the list? Please post some code Commented Feb 23, 2012 at 17:10

5 Answers 5

17

If you just want to clear the selected items then use the code below:

        ListBox1.ClearSelection();

        //or

        foreach (ListItem listItem in ListBox1.Items)
        {
            listItem.Selected = false;
        }

If you mean to what to actually remove the items, then this is the code for you..

        List<ListItem> itemsToRemove = new List<ListItem>();

        foreach (ListItem listItem in ListBox1.Items)
        {
            if (listItem.Selected)
                itemsToRemove.Add(listItem);
        }

        foreach (ListItem listItem in itemsToRemove)
        {
            ListBox1.Items.Remove(listItem);
        }
Sign up to request clarification or add additional context in comments.

7 Comments

I think he's asking how to remove the selected items, not how to remove the highlight.
@MattT - Yeah just thought that! was updating my answer :) thanks
I get this Error 1 'System.Web.UI.WebControls.ListBox' does not contain a definition for 'Selected' and no extension method 'Selected' accepting a first argument of type 'System.Web.UI.WebControls.ListBox' could be found (are you missing a using directive or an assembly reference?)
@RonaldMcDonald Can you post your code? It sounds like you are looking for a 'Selected' property on the ListBox, rather than the List Item...
We could probably optimize the code to this, but I don't have my IDE handy to double check. foreach(ListItem listItem in ListBox1.SelectedItems) { ListBox1.Items.Remove(listItem); }
|
1

Try this to remove selected items from list box.

 protected void Remove_Click(object sender, EventArgs e)
{
    while (ListBox.GetSelectedIndices().Length > 0)
    {
        ListBox.Items.Remove(ListBox.SelectedItem); 
    }
}

Comments

0

I tried some experiments and the technique below works. It's not very efficient, in that it requeries the listbox on each iteration, but it gets the job done.

        while (myListBox.SelectedIndex != -1)
        {
            ListItem mySelectedItem = (from ListItem li in myListBox.Items where li.Selected == true select li).First();
            myListBox.Items.Remove(mySelectedItem);
        };

Comments

0

Why not simply use the Items.Remove and pass the selected item string value.

ListBox1.Items.Remove(ListBox1.SelectedItem.ToString());

1 Comment

there could be multiple selected items.
-2
protected void ButtonRemoveSelectedItem_Click(object sender, EventArgs e)
{
    int position = 0;

    for (byte i = 0; i < ListBox2.Items.Count; i++)
    { 
        position = ListBox2.SelectedIndex ;
    }

    ListBox2.Items.RemoveAt(position);
}

1 Comment

Hi, welcome to Stack Overflow! Code-only answers are generally not welcomed in the community, because they leave readers the task of figuring out what's going on. Please provide some explanation of how and why your code works. Take a look at how to answer for more info.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.