protected void Delete_Click(object sender, EventArgs e)
{
List<String> li_Items_Selected = new List<string>();
foreach(ListItem li in ListBox1.Items)
{
if (li.Selected)
{
li_Items_Selected.Add(li.Value);
}
}
if (li_Items_Selected.Count > 0)
{
for (int i = 0; i < li_Items_Selected.Count; i++)
ListBox1.Items.Remove(li_Items_Selected[i]);
}
}
Neethus
Member
15 Points
68 Posts
delete selected items from ListBox
Jan 31, 2013 03:26 AM|LINK
hi all
How to delete selected items from a ListBox
shivalthakur
Participant
1837 Points
531 Posts
Re: delete selected items from ListBox
Jan 31, 2013 04:00 AM|LINK
hi you can try this
<asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple"> <asp:ListItem>Ajay</asp:ListItem> <asp:ListItem>Anoop</asp:ListItem> <asp:ListItem>Ankush</asp:ListItem> <asp:ListItem>Bawa</asp:ListItem> <asp:ListItem>Chitra</asp:ListItem> </asp:ListBox> <br /> <br /> <asp:Button ID="Delete" runat="server" Text="Delete Selected" onclick="Delete_Click" />Code Behind
protected void Delete_Click(object sender, EventArgs e) { List<String> li_Items_Selected = new List<string>(); foreach(ListItem li in ListBox1.Items) { if (li.Selected) { li_Items_Selected.Add(li.Value); } } if (li_Items_Selected.Count > 0) { for (int i = 0; i < li_Items_Selected.Count; i++) ListBox1.Items.Remove(li_Items_Selected[i]); } }hope this will help
Response.Write("Success");
Best Of Luck
Shival Thakur
Neethus
Member
15 Points
68 Posts
Re: delete selected items from ListBox
Jan 31, 2013 04:29 AM|LINK
This code is not working. selected items are not deleted
g_mani
Contributor
2055 Points
586 Posts
Re: delete selected items from ListBox
Jan 31, 2013 04:39 AM|LINK
Hi try this,
call this javascript function in button OnClientClick evenet,
<script type='text/javascript'> function removeItem(selectbox) { var i; for(i=selectbox.options.length-1;i>=0;i--) { if(selectbox.options[i].selected) selectbox.remove(i); } } </script>Please Mark as Answer If this is helpful.
Sujeet Saste
Contributor
3008 Points
572 Posts
Re: delete selected items from ListBox
Jan 31, 2013 04:55 AM|LINK
Try following :
.aspx :
<asp:ListBox ID="lstTest" runat="server" SelectionMode="Multiple" > <asp:ListItem>One</asp:ListItem> <asp:ListItem>Two</asp:ListItem> <asp:ListItem>Three</asp:ListItem> <asp:ListItem>Four</asp:ListItem> <asp:ListItem>Five</asp:ListItem> </asp:ListBox> <br /> <asp:Button ID="btnDel" runat="server" Text="Delete" OnClick="btnDel_Click" />.cs :
protected void btnDel_Click(object sender, EventArgs e) { for (int i = 0; i < lstTest.Items.Count; i++) { if (lstTest.Items[i].Selected == true) { lstTest.Items.Remove(lstTest.Items[i]); } } }Feel free to ask if have any query remains....
Do FEAR (Face Everything And Rise)
Please mark as Answer if my post helps you..!
My Blog