csharp

Asp.net checkboxlist, select or deselect all list items in C#

In this article, we will learn Asp.net checkboxlist, select or deselect all list items in C#. Before Select or deselect all list Items of a CheckBoxList control learn about What is CheckboxList?

CheckboxList:
1. CheckBoxList is just a collection of ListItem objects.
2. We can add Items to the CheckBoxList in the HTML source or in the code behind file

Why Use CheckBoxList control ?
if you want to select more than one option based on users choice, then a CheckBoxList control can be used.

Complete Demo about Asp.net checkboxlist, select or deselect all list items in C#
Step by step follow for Asp.net checkboxlist, select or deselect all list items in C#

Step 1:Now Open Visual Studio and create the project as:
“Start” –> “All Programs”- “Microsoft Visual Studio 2012”.
“File” – “New Project” – “C#” – “Empty Project” (to avoid adding a master page).
Give the Project name such as “SelectCheckboxDemo” or another as you wish and specify the location.
Then right-click on your project name – Select Add option–>Select “New Item” –> “WebForm1.aspx” page.

Step 2:Open WebForm1.aspx page and write this code.
In “WebForm1.aspx” page drag and drop one of an ASP.Net CheckBoxList control, and two Button and The Button has been assigned a Click event handler.
WebForm1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="SelectCheckboxDemo.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:CheckBoxList ID="checkboxlist" runat="server">
        <asp:ListItem Text="Cricket" Value="1"></asp:ListItem>
        <asp:ListItem Text="Football" Value="2"></asp:ListItem>
        <asp:ListItem Text="Swimming" Value="3"></asp:ListItem>
        <asp:ListItem Text="Singing" Value="4"></asp:ListItem>

    </asp:CheckBoxList>
        <asp:Button ID="selectall" runat="server" Text="Select All" OnClick="selectall_Click" />
        &nbsp;
       <asp:Button ID="deselectall" runat="server" Text="Deselect All" OnClick="deselectall_Click" />
    </div>
    </form>
</body>
</html>

Step 5:Open WebForm1.aspx.cs file to write code for Asp.net checkboxlist, select or deselect all list items in C#.
WebForm1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SelectCheckboxDemo
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void deselectall_Click(object sender, EventArgs e)
{
foreach (ListItem item in checkboxlist.Items)
{
item.Selected = false;
}
}

protected void selectall_Click(object sender, EventArgs e)
{
foreach (ListItem item in checkboxlist.Items)
{
item.Selected = true;
}
}
}
}

Any question and suggestion related to this article please comment me. Thank you

Demo

Leave a Reply

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