In this article I will explain a tutorial with example, how to use the OnSelectedIndexChanged event of CheckBoxList control in ASP.Net using C# and VB.Net.
This article will also explain how to get the Selected Text and Value inside the OnSelectedIndexChanged event of CheckBoxList control in ASP.Net using C# and VB.Net.
Database
This article makes use of a table named Fruits whose schema is defined as follows.
The Fruits table has the following records.
Note: You can download the database table SQL by clicking the download link below.
HTML Markup
The HTML Markup consists of an ASP.Net CheckBoxList. The CheckBoxList has been assigned an OnSelectedIndexChanged event handler and the AutoPostBack property is set to True.
The AutoPostBack property when set to True, it will raise PostBack when an item is selected or changed in the CheckBoxList and the OnSelectedIndexChanged event will be raised.
If the AutoPostBack property is not set to True then even though the OnSelectedIndexChanged event handler is assigned, still the OnSelectedIndexChanged event will not be raised.
<asp:CheckBoxList ID="chkFruits" runat="server"
OnSelectedIndexChanged = "OnCheckBox_Changed" AutoPostBack = "true">
</asp:CheckBoxList>
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Populating CheckBoxList control from database in ASP.Net
Inside the Page Load event of the page, the CheckBoxList is populated with the records of the Fruits Table.
The FruitId and the FruitName column values are fetched from the database using SqlDataReader and are assigned to the DataTextField and DataValueField properties of the CheckBoxList control.
DataTextField – The values of the column set as DataTextField are visible to the user.
DataValueField – The values of the column set as DataValueField are not visible to the user. Generally ID or Primary Key columns are set as values in order to uniquely identify a CheckBoxList Item.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string constr = ConfigurationManager.ConnectionStrings["Constring"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string query = "SELECT FruitName, FruitId FROM Fruits";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
chkFruits.DataSource = cmd.ExecuteReader();
chkFruits.DataTextField = "FruitName";
chkFruits.DataValueField = "FruitId";
chkFruits.DataBind();
con.Close();
}
}
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim constr As String = ConfigurationManager.ConnectionStrings("Constring").ConnectionString
Using con As New SqlConnection(constr)
Dim query As String = "SELECT FruitName, FruitId FROM Fruits"
Using cmd As New SqlCommand(query)
cmd.CommandType = CommandType.Text
cmd.Connection = con
con.Open()
chkFruits.DataSource = cmd.ExecuteReader()
chkFruits.DataTextField = "FruitName"
chkFruits.DataValueField = "FruitId"
chkFruits.DataBind()
con.Close()
End Using
End Using
End If
End Sub
The OnSelectedIndexChanged event handler of the ASP.Net CheckBoxList
When an item is selected in the CheckBoxList, the OnSelectedIndexChanged event handler is raised.
The Value and the Text part of the Selected Items of the ASP.Net CheckBoxList is fetched and displayed using JavaScript Alert message box.
C#
protected void OnCheckBox_Changed(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 OnCheckBox_Changed(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
Demo
Downloads