How to Get all Selected Items or Checked Items of CheckBoxList in Asp.net C# VB.Net

CheckBoxList is a most important control in our development. More often we need to do more sophisticated work on CheckBoxList. Here in this article I will explain “How to Get all Selected Items or Checked Items of CheckBoxlist from ServerSide Code“. Though it’s a bit easy but to save your valuable time I am giving you a complete example with source code. Here first I will bind the CheckBoxList & then loop through each CheckBox to check whether the CheckBox is Selected or not? If found selected then write down the Selected Value & Selected Text of CheckBoxes into a Label Control.

Related Articles:
1. Jquery Click Event to get Selected Items or Selected Values of Checkboxlist Asp.Net C# VB.Net
2. Javascript onclick Event to get CheckBoxList Selected Value Selected Index Selected Text ASP.Net C# VB.Net
3. How to Bind Update CheckBoxList from SQL Server Database bit Datatype in ASP.Net using C# VB.Net

Objective/Outcome:
How to Get all Selected Items of CheckBoxList in Asp.net CSHARP VB.Net

To complete the Example create a sample table like below:

CREATE TABLE [dbo].[Article](
    [ArticleID] [int] NOT NULL,
    [Title] [varchar](max) NOT NULL,
    [IsPublished] [bit] NOT NULL,
 CONSTRAINT [PK_Article] PRIMARY KEY CLUSTERED 
([ArticleID] ASC)
)

Insert Some Data to Test:

INSERT INTO ARTICLE VALUES(1,'Populate SQL Data Into Checkboxlist',1)
INSERT INTO ARTICLE VALUES(2,'Checkboxlist data population example',0)
INSERT INTO ARTICLE VALUES(3,'C# Checkboxlist lesson',1)
INSERT INTO ARTICLE VALUES(4,'VB.Net Checkboxlist Example',0)
INSERT INTO ARTICLE VALUES(5,'Asp.net Checkboxlist load data',0)

Now Modify the Web.Config file to prepare Connection String:

<configuration>
  <connectionStrings>
    <add name="DBConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=Test;Trusted_Connection=yes;" providerName="System.Data.SqlClient"/>
    <!--<add name="BONConnection" connectionString="Data Source=XXX.com;Initial Catalog=DBNAME;User Id=UserName;Password=YourPassword;" providerName="System.Data.SqlClient" />-->
  </connectionStrings>
...................
...................

Note: If you use windows authentication then use the first key. Otherwise use second key line. Update server name, Database name, User name, Password as per your settings.

Now Prepare the HTML Markup like below:

    <asp:CheckBoxList ID="CheckBoxList1" runat="server">
    </asp:CheckBoxList><br />
    <hr />
    <asp:Button ID="cmd_Get_Checked_Items" runat="server" 
        Text="Get All Checked Items" onclick="cmd_Get_Checked_Items_Click" /><br /><br />
    <asp:Label ID="lbl_Selected_Items" runat="server" Text=""></asp:Label>

Now Bind Data into CheckBoxList under Page_Load Event:
C# Code:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //How to Get all Selected Items or Checked Items of CheckBoxList in Asp.net C# VB.Net
            DataTable dt = new DataTable();
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString))
            {
                SqlDataAdapter ad = new SqlDataAdapter("SELECT ArticleID,Title from Article", conn);
                ad.Fill(dt);
            }
            CheckBoxList1.DataSource = dt;
            CheckBoxList1.DataTextField = "Title";
            CheckBoxList1.DataValueField = "ArticleID";
            CheckBoxList1.DataBind();

        }
    }

Note: Don’t forget to add “System.Data”, “System.Data.SqlClient”, “System.Configuration” Namespaces.

VB.Net Code:

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            'How to Get all Selected Items or Checked Items of CheckBoxList in Asp.net C# VB.Net
            Dim dt As New DataTable()
            Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("DBConnection").ConnectionString)
                Dim ad As New SqlDataAdapter("SELECT ArticleID,Title from Article", conn)
                ad.Fill(dt)
            End Using
            CheckBoxList1.DataSource = dt
            CheckBoxList1.DataTextField = "Title"
            CheckBoxList1.DataValueField = "ArticleID"

            CheckBoxList1.DataBind()
        End If
    End Sub

Note: Don’t forget to add “System.Data”, “System.Data.SqlClient”, “System.Configuration” Namespaces.

Get all Selected Items or Checked Items of CheckBoxList:
C# Code:

    protected void cmd_Get_Checked_Items_Click(object sender, EventArgs e)
    {
        string sCheckedValue = "";
        foreach (ListItem oItem in CheckBoxList1.Items)
        {
            if (oItem.Selected)
            {
                if (sCheckedValue == "")
                    sCheckedValue = "Selected Value : "+oItem.Value + " Selected Text: "+oItem.Text;
                else
                    sCheckedValue += "<br/>Selected Value : " + oItem.Value + " Selected Text: " + oItem.Text;
            }
        }
        lbl_Selected_Items.Text = sCheckedValue;
    }

VB.Net Code:

    Protected Sub cmd_Get_Checked_Items_Click(sender As Object, e As EventArgs)
        Dim sCheckedValue As String = ""
        For Each oItem As ListItem In CheckBoxList1.Items
            If oItem.Selected Then
                If sCheckedValue = "" Then
                    sCheckedValue = ("Selected Value : " + oItem.Value & " Selected Text: ") + oItem.Text
                Else
                    sCheckedValue += ("<br/>Selected Value : " + oItem.Value & " Selected Text: ") + oItem.Text
                End If
            End If
        Next
        lbl_Selected_Items.Text = sCheckedValue
    End Sub

Hope now you can loop through in each CheckBox of a CheckBoxList & can get Selected Items or Selected Values of those Checked CheckBoxes as per your need.

Download Code Example C#        Download Code Example VB.Net

One thought on “How to Get all Selected Items or Checked Items of CheckBoxList in Asp.net C# VB.Net

  • July 6, 2015 at 6:09 am
    Permalink

    when i try write this coding CheckBoxList1.DataSource = dt CheckBoxList1.DataTextField = “Title” CheckBoxList1.DataValueField = “ArticleID” CheckBoxList1.DataBind() the checkboxlist1 is undeclared.. eventhough i already make it in .aspx

    Reply

Leave a Reply

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

AlphaOmega Captcha Classica  –  Enter Security Code