asp.net c# tutorials and examples code for beginners

How to remove all items from CheckBoxList in asp.net c#

CheckBoxList remove all items programmatically
asp.net list web server control checkboxlist provides a multi selection checkbox group. checkboxlist ListItem objects contains in a Items collection. we can add or remove items from checkboxlist using adding or removing items from this Items collection. asp.net developers can remove all items from checkboxlist by calling Items Clear method (ListItemCollection.Clear method). Clear method remove (delete) all ListItem objects from the Items collection.

the following asp.net c# example source code demonstrate us how can we remove all items from checkboxlist programmatically at run time.
CheckBoxListItemClear.aspx
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        CheckBoxList1.Items.Clear();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to remove all item from CheckBoxList, items clear</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Green">CheckBoxList: Items Clear</h2>
        <asp:Label 
             ID="Label1"
             runat="server"
             Font-Bold="true"
             ForeColor="DarkGoldenrod"
             Text="asp.net controls"
             >
        </asp:Label>
        <asp:CheckBoxList 
             ID="CheckBoxList1"
             runat="server"
             BorderColor="Crimson"
             BorderWidth="2"
             >
             <asp:ListItem>Timer</asp:ListItem>
             <asp:ListItem>Wizard</asp:ListItem>
             <asp:ListItem>ConnectionsZone</asp:ListItem>
             <asp:ListItem>Table</asp:ListItem>
             <asp:ListItem>ImportCatalogPart</asp:ListItem>
        </asp:CheckBoxList>
        <br /><br />
        <asp:Button 
             ID="Button1"
             runat="server"
             Font-Bold="true"
             ForeColor="DarkGoldenrod"
             Text="Clear Item"
             OnClick="Button1_Click"
             />
    </div>
    </form>
</body>
</html>






Related asp.net example