asp.net listbox remove selected items
ListBox is an asp.net list web server control that allow users to select single item or multiple items
at a time. we can delete (remove) an item from listbox items collection programmatically by using its Items.Remove() method.
ListItemCollection.Remove(ListItem) method allow us to remove the specified item from the items collection.
In this example, we will remove users selected multiple items programmatically at a time by a button click event. to do this, first we apply a linq query on button click event to get a list of listbox selected ListItem objects. next we loop through the selected ListItem objects list and delete (remove) all selected items from the listbox items collection. finally we get a listbox where users last selected items are removed.
The following asp.net c# example code demonstrate us how can we remove selected items from listbox programmatically in an asp.net application.
In this example, we will remove users selected multiple items programmatically at a time by a button click event. to do this, first we apply a linq query on button click event to get a list of listbox selected ListItem objects. next we loop through the selected ListItem objects list and delete (remove) all selected items from the listbox items collection. finally we get a listbox where users last selected items are removed.
The following asp.net c# example code demonstrate us how can we remove selected items from listbox programmatically in an asp.net application.
listbox-remove-selected-items.aspx
<%@ Page Language="C#" AutoEventWireup="true"%>
<!DOCTYPE html>
<script runat="server">
protected void Button1_Click(object sender, System.EventArgs e)
{
//linq query to get a list of all selected items from listbox
var items =(from ListItem li in ListBox1.Items
where li.Selected == true
select li).ToList<ListItem>();
Label1.Text = "you selected....<br />";
foreach (ListItem li in items)
{
Label1.Text += li.Text + " | " + li.Value + "<br />";
//remove/delete current selected item from listbox
ListBox1.Items.Remove(li);
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>asp.net listbox remove selected items</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
asp.net example - listbox remove selected items
</h2>
<hr width="550" align="left" color="Gainsboro" />
<asp:Label
ID="Label1"
runat="server"
Text="select item(s) from listbox."
Font-Size="X-Large"
Width="350"
>
</asp:Label>
<br /><br />
<asp:ListBox
ID="ListBox1"
runat="server"
AutoPostBack="false"
Font-Size="X-Large"
SelectionMode="Multiple"
Font-Names="Comic Sans MS"
Rows="5"
>
<asp:ListItem Text="Planalto Woodcreeper" Value="1"></asp:ListItem>
<asp:ListItem Text="Olivaceous Woodcreeper" Value="2"></asp:ListItem>
<asp:ListItem Text="Red-billed Scythebill" Value="3"></asp:ListItem>
<asp:ListItem Text="Superb Lyrebird" Value="4"></asp:ListItem>
<asp:ListItem Text="Green Catbird" Value="5"></asp:ListItem>
</asp:ListBox>
<br />
<asp:Button
ID="Button1"
runat="server"
Text="remove selected item(s)"
OnClick="Button1_Click"
Height="40"
Font-Bold="true"
/>
</div>
</form>
</body>
</html>


- How to sort DropDownList items
- How to sort DropDownList items alphabetically
- How to show horizontal ScrollBar in a ListBox
- How to get ListBox multiple selected items
- How to count ListBox selected items
- How to set default selected items in a ListBox
- How to disable specific items in a ListBox
- How to check whether an item exists in a ListBox
- How to add attributes to a ListBox items
- How to set RadioButtonList items vertical alignment