Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Delete (Remove) Multiple Selected Items from Listbox in Asp.net on Button Click using C#, VB.NET

Mar 2, 2015
Introduction:

Here I will explain how to remove or
delete multiple selected items from listbox in on button click in asp.net using c#, vb.net with example or remove or delete multiple items from listbox items button click in asp.net using c#, vb.net.

Description:

In previous post I explained
add textbox values to listbox in asp.net on button click, create and consume webservice in asp.net, simple cursor example in sql server, sitemap control example, send html page as mail body in asp.net, how to send mail with attachment in asp.net and many more articles related to asp.net using c#, vb.net. Now I will explain how to delete or remove multiple selected items from listbox on button click in asp.net using c#, vb.net with example.

To delete selected listbox items in asp.net on button click using c#, vb.net we need to write aspx code like as shown below


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Delete Multiple Selected Listbox Items in asp.net on Button Click</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<b>Listbox Items:</b> <asp:ListBox ID="lbColors" runat="server" SelectionMode="Multiple" Height="134px" Width="237px">
<asp:ListItem>Blue</asp:ListItem>
<asp:ListItem>Red</asp:ListItem>
<asp:ListItem>Orange</asp:ListItem>
<asp:ListItem>Green</asp:ListItem>
<asp:ListItem>Black</asp:ListItem>
<asp:ListItem>White</asp:ListItem>
<asp:ListItem>Brown</asp:ListItem>
<asp:ListItem>Navy Blue</asp:ListItem>
</asp:ListBox>
<br />
<asp:Button ID="btnDelete" runat="server" Text="Delete Items" onclick="btnDelete_Click" />
</div>
</form>
</body>
</html>

After completion of aspx page write the add following namespaces in codebehind

C# Code


using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;

VB.NET Code


Imports System.Collections.Generic
Imports System.Web.UI.WebControls

After completion of adding namespaces you need to write the code like as shown below

C# Code


protected void btnDelete_Click(object sender, EventArgs e)
{
List<ListItem> lstSelectedCities = new List<ListItem>();
foreach (ListItem liItems in lbColors.Items)
{
if (liItems.Selected == true)
{
lstSelectedCities.Add(liItems);
}
}
foreach (ListItem liSelected in lstSelectedCities)
{
lbColors.Items.Remove(liSelected);
}
}

VB.NET Code


Imports System.Collections.Generic
Imports System.Web.UI.WebControls
Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim lstSelectedCities As New List(Of ListItem)()
For Each liItems As ListItem In lbColors.Items
If liItems.Selected = True Then
lstSelectedCities.Add(liItems)
End If
Next
For Each liSelected As ListItem In lstSelectedCities
lbColors.Items.Remove(liSelected)
Next
End Sub
End Class

Demo


If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

0 comments :

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.