How to Remove Selected Item from CheckBoxList in ASP.Net

In this ASP.Net tutorials we are to learn how to Remove Selected Item from CheckBoxList control Programmatically in ASP.Net with C# Language.

STEP 1 – Design the ASP.Net webpage with one CheckBoxList Control, One Button Control and One Label Control:

<table style=”width: 464px”>
<tr>
<td colspan=”1″ style=”width: 257px; height: 24px”>
<strong><span style=”font-size: 14pt; color: #6600cc; font-family: Arial”>&nbsp;Remove
Selected Item – CheckBoxList</span></strong></td>
</tr>
<tr>
<td style=”width: 257px; height: 24px; text-align: center;”>
&nbsp;
<asp:Button ID=”Button1″ runat=”server” OnClick=”Button1_Click” Text=”Remove Selected” Font-Bold=”True” Font-Size=”Medium” />&nbsp;</td>
</tr>
<tr>
<td style=”width: 257px; height: 22px; text-align: center;”>
<asp:CheckBoxList ID=”CheckBoxList1″ runat=”server” Width=”168px”>
<asp:ListItem Value=”1″>ABC</asp:ListItem>
<asp:ListItem Value=”2″>DEF</asp:ListItem>
<asp:ListItem Value=”3″>GHI</asp:ListItem>
<asp:ListItem Value=”4″>PQR</asp:ListItem>
<asp:ListItem Value=”5″>STU</asp:ListItem>
<asp:ListItem Value=”6″>XYZ</asp:ListItem>
</asp:CheckBoxList>
<asp:Label ID=”Label1″ runat=”server”></asp:Label></td>
</tr>
<tr>
<td style=”width: 257px; height: 22px; text-align: center”>
</td>
</tr>
</table>

– The ASP.Net CheckBoxList Control Remove Item Example output is:

 Remove selected item from CheckBoxList control in ASP.Net with C#

Remove selected item from CheckBoxList control in ASP.Net with C#

STEP 2 – Now select the item from CheckBoxList Contol.

Remove selected item from CheckBoxList control in ASP.Net with C#
Remove selected item from CheckBoxList control in ASP.Net with C#

STEP 3 – Write the Below code on Button Click Event at Code behind page of ASP.Net for Removing Selected Item:

protected void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{
if (CheckBoxList1.Items[i].Selected == true)
{
CheckBoxList1.Items.RemoveAt(i);
Label1.Text = “Item Removed”;
}
}
}

–  The ASP.Net Remove Selected Item From CheckBoxlist Control Exmaple output is:

Remove selected item from CheckBoxList control in ASP.Net with C#
Remove selected item from CheckBoxList control in ASP.Net with C#

I hope you enjoy this ASP.Net Exmaple of Remove selected item from CheckBoxList Control.

Leave a Reply

Your email address will not be published. Required fields are marked *