asp.net - Remove multiple items from a CheckBoxList

Remove items from CheckBoxList
The following asp.net c# example code demonstrate us how can we remove multiple items from CheckBoxList server control's programmatically at a time. CheckBoxList is an asp.net list web server control which allow web users to select/checked one or more items at a time.

CheckBoxList control contains one or more items as presented CheckBox. CheckBoxList's all items are exists in an items collection. So we can manage CheckBoxList items by .net framework's Collection<T> Class methods and properties. Collection<T> Class Remove() method allow us to remove/delete an item from items collection.

To remove an item from CheckBoxList control's we can call the Remove() method as CheckBoxListID.Items.Remove(ListItem). We need to pass the specified ListItem object to remove from CheckBoxList items collection.

In this tutorial we removed multiple items from CheckBoxList control's at a time which ListItem object's are currently selected/checked. To do this, first we initialize an ArrayList and populate the ArrayList with CheckBoxList selected items Text . Next, we loop through the ArrayList items and remove all corresponded items from CheckBoxList control. Finally, CheckBoxList control display only those items which are not removed.
CheckBoxListRemoveItem.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CheckBoxListRemoveItem.aspx.cs" Inherits="CheckBoxListRemoveItem" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>asp.net CheckBoxList: how to remove list item programmatically</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Red">CheckBoxList: Remove List Item</h2>
        <asp:Label 
            ID="Label1" 
            runat="server"
            >
        </asp:Label>
        <br /><br />
        <asp:CheckBoxList 
            ID="CheckBoxList1"
            runat="server"
            RepeatColumns="3"
            BorderColor="Blue"
            BorderWidth="2"
            BackColor="DodgerBlue"
            ForeColor="Snow"
            >
            <asp:ListItem>Red</asp:ListItem>
            <asp:ListItem>Orange</asp:ListItem>
            <asp:ListItem>Yellow</asp:ListItem>
            <asp:ListItem>Blue</asp:ListItem>
            <asp:ListItem>White</asp:ListItem>
        </asp:CheckBoxList>
        <br />
        <asp:Button 
            ID="Button1"
            runat="server"
            Font-Bold="true"
            ForeColor="DarkBlue"
            Text="Remove Selected Item(s)"
            OnClick="Button1_Click"
            />
    </div>
    </form>
</body>
</html>
CheckBoxListRemoveItem.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 CheckBoxListRemoveItem : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
            Label1.ForeColor = System.Drawing.Color.OrangeRed;
            Label1.Font.Bold = true;
        }
    }

    protected void Button1_Click(object sender, EventArgs e) {
        Label1.Text = "Items removed:";
        ArrayList ListItems = new ArrayList();
        foreach (ListItem li in CheckBoxList1.Items)
        {
            if (li.Selected == true)
            {
                ListItems.Add(li.Text.ToString());
            }
        }
        for (int i = 0; i < ListItems.Count; i++)
        {
            string currentItem =(string) ListItems[i];
            ListItem li = new ListItem();
            li.Text = currentItem;
            CheckBoxList1.Items.Remove(li);
            Label1.Text += " " + currentItem; 
        }
    }
}
More asp.net examples