In this article I will explain with an example, how to populate (bind) CheckBoxList control using SqlDataSource in ASP.Net.
The SqlDataSource control will populate the CheckBoxList control from SQL Server database in ASP.Net.
This article will also explain how to fetch the Selected Item Text and Value of the CheckBoxList which is populated using SqlDataSource in ASP.Net.
 
 
Database
This article makes use of a table named Fruits whose schema is defined as follows.
Populate (Bind) CheckBoxList control using SqlDataSource in ASP.Net
 
The Fruits table has the following records.
Populate (Bind) CheckBoxList control using SqlDataSource in ASP.Net
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 
CheckBoxList control and its Properties
The CheckBoxList control makes use of the following properties when it is being populated using the SqlDataSource control.
DataSourceID – The ID of the SqlDataSource control is set as value to this property, this way the CheckBoxList control determines its source of data.
 
DataTextField – The name of the column to be set as Text in CheckBoxList. These values will be visible to end user.
 
DataValueField – The name of the column to be set as Value in CheckBoxList. These values will not be visible to end user.
 
 
HTML Markup
The following HTML Markup consists of an ASP.Net CheckBoxList control, a Button and ASP.Net SqlDataSource control.
The SqlDataSource control is set with the following properties.
1. ConnectionString – Name of the Connection String setting in the Web.Config file.
2. SelectCommand – The Select statement to fetch the records from the Fruits table.
The ID of the SqlDataSource control is set as DataSourceID of the CheckBoxList control.
<asp:CheckBoxList ID="chkFruits" runat="server" DataSourceID="SqlDataSource1"
    DataTextField="FruitName" DataValueField="FruitId">
</asp:CheckBoxList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:constr %>"
    SelectCommand="SELECT FruitName, FruitId FROM Fruits"></asp:SqlDataSource>
<br />
<asp:Button ID="btnSubmit" Text="Submit" runat="server" OnClick="Submit" />
 
 
Getting the Selected Text and Value of the ASP.Net CheckBoxList
When the Button is clicked, the Value and the Text part of the Selected Item of the ASP.Net CheckBoxList is fetched and displayed using JavaScript Alert message box.
C#
protected void Submit(object sender, EventArgs e)
{
    string message = "";
    foreach (ListItem item in chkFruits.Items)
    {
        if (item.Selected)
        {
            message += "Value: " + item.Value;
            message += " Text: " + item.Text;
            message += "\\n";
        }
    }
 
    ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + message + "');", true);
}
 
VB.Net
Protected Sub Submit(sender As Object, e As EventArgs)
    Dim message As String = ""
    For Each item As ListItem In chkFruits.Items
        If item.Selected Then
            message &= "Value: " & item.Value
            message &= " Text: " & item.Text
            message &= "\n"
        End If
    Next
 
    ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('" & message & "');", True)
End Sub
 
 
Screenshot
Populate (Bind) CheckBoxList control using SqlDataSource in ASP.Net
 
 
Demo
 
 
Downloads