asp.net ListBox: how to remove multiple list items programmatically
ListBoxRemoveMultipleItems.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ListBoxRemoveMultipleItems.aspx.cs" Inherits="ListBoxRemoveMultipleItems" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>asp.net ListBox: how to remove multiple list items programmatically</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Green">ListBox: Remove Multiple List Items</h2>
<asp:Label
ID="label1"
runat="server"
ForeColor="DarkSalmon"
Font-Italic="true"
Font-Size="Medium"
>
</asp:Label>
<br />
<asp:ListBox
ID="ListBox1"
runat="server"
ForeColor="White"
BackColor="Crimson"
SelectionMode="Multiple"
>
<asp:ListItem>Crimson</asp:ListItem>
<asp:ListItem>DarkSalmon</asp:ListItem>
<asp:ListItem>OrangeRed</asp:ListItem>
<asp:ListItem>Green</asp:ListItem>
<asp:ListItem>White</asp:ListItem>
</asp:ListBox>
<br />
<asp:Button
ID="Button1"
runat="server"
Text="Remove selected Item(s)"
OnClick="Button1_Click"
ForeColor="DarkSalmon"
Font-Bold="true"
/>
</div>
</form>
</body>
</html>
ListBoxRemoveMultipleItems.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class ListBoxRemoveMultipleItems : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
label1.Text = "Items removed:";
ArrayList itemList = new ArrayList();
foreach (ListItem li in ListBox1.Items)
{
if (li.Selected == true)
{
itemList.Add(li.Text.ToString());
}
}
for (int i = 0; i < itemList.Count; i++)
{
string currentItem =(string) itemList[i];
ListItem li = new ListItem();
li.Text = currentItem;
ListBox1.Items.Remove(li);
label1.Text += " " + currentItem;
}
}
}
- asp.net RegularExpressionValidator: how to validate email
- asp.net RadioButtonList: how to remove list item programmatically
- asp.net RadioButtonList: how to add list item programmatically
- asp.net RequiredFieldValidator: how to use
- asp.net RangeValidator: how to use
- asp.net RadioButtonList Validation: how to validate
- asp.net ListBox Validation: how to validate
- asp.net DropDownList Validation: how to validate
- asp.net CompareValidator: how to use
- asp.net TextBox: how to use OnTextChanged event