jQuery to highlight repeater selected row based on checkbox selection in Asp.Net C#,VB

Introduction: In this article I am going to share how to bind repeater and add checkbox with each record in repeater and highlight row based on checkbox selection in asp.net using jquery.
jQuery to highlight repeater selected row based on checkbox selection in asp.net
Click on image to enlarge

Description:  Its very common requirement to show checkbox along with each record in repeater to select specific records. It is very easy task but here I have shown how to change color of the selected row based on checkbox check uncheck using jquery to highlight selected row and make it look more attractive as shown in demo image above

Implementation: Let’s create a page to demonstrate the concept. 
  • First of all create a table and add dummy data into it using the following script:

CREATE TABLE tbBookDetails
                BookId INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
                BookName         VARCHAR(100),
                Author                 VARCHAR(100),
                Publisher            VARCHAR(100),
                BookpPrice       DECIMAL(10,2)

('Asp.Net','Ajay','Rozy Publication',1200),
('C#.Net','Bhavuk','Jai Publication',1000),
('VB.Net','Nancy','Rozy Publication',970),
('MVC','Sahil','Amar Publication',1480),
('JAVA','Supreet','Sam Publication',850),
('PHP','Parvesh','Maya Publication',800)

  • Now create a stored procedure to fetch all the records from table to bind in repeater data control

                SET NOCOUNT ON;
                SELECT * FROM tbBookDetails

HTML Source
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script type="text/javascript">
        function HighlightSelectedRow(cb) {
            var selectedRow = $(cb).parent().parent();
            if ($(cb).is(":checked")) {
                $(selectedRow).prop('class', 'highlight')
            else {
                $(selectedRow).prop('class', 'normal')
    <style type="text/css">
        .highlight {
            background-color: #feddc5;
        .normal {
            background-color: #FFFFFF;
    <form id="form1" runat="server">
            <table border="1" cellspacing="2" cellpadding="2" style="width: 500px;">
                <tr style="background-color: #E36C0A; color: #FFFFFF; height: 30px;">
                    <td>Book Name</td>
                <asp:Repeater ID="rptBookDetails" runat="server">
                            <td style="text-align: center;">
                                <%#Container.ItemIndex+1 %>
                                <asp:CheckBox ID="chkSelect" runat="server" OnClick="HighlightSelectedRow($(this));" />
                                <asp:Literal ID="ltrlBookName" runat="server" Text='<%#Eval("BookName") %>'></asp:Literal>
                                <asp:Literal ID="ltrlAuthor" runat="server" Text='<%#Eval("Author") %>'></asp:Literal>
                                <asp:Literal ID="ltrlPublisher" runat="server" Text='<%#Eval("Publisher") %>'></asp:Literal>
                                <asp:Literal ID="ltrlPrice" runat="server" Text='<%#Eval("BookPrice") %>'></asp:Literal>

  • Now In the <configuration> tag of web.config file add your connectionstring as:
    <add name="conStr" connectionString="Data Source=LALIT-PC;Initial Catalog=MyDataBase;Integrated Security=True"/>
Note: Replace Data Source and Catalogue i.e. Database name as per your application

Asp.Net code to bind repeater from sql server database table

In code behind file(.aspx.cs)write the code as:
using System;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

//Create Connection object and get connection string defined in web.config file
    SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)

    protected void BindRepeater()
            using (sqlCon)
                using (SqlCommand cmd = new SqlCommand("spBookDetails", sqlCon))
                    cmd.CommandType = CommandType.StoredProcedure;
                    using (SqlDataReader dr = cmd.ExecuteReader())
                        if (dr.HasRows)
                            rptBookDetails.DataSource = dr;

Asp.Net VB Code to bind repeater from sql server database table

In code behind file(.aspx.vb)write the code as:
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient

'Create Connection object and get connection string defined in web.config file
    Private sqlCon As New SqlConnection(ConfigurationManager.ConnectionStrings("conStr").ConnectionString)
  Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
        End If
    End Sub
    Protected Sub BindRepeater()
            Using sqlCon
                Using cmd As New SqlCommand("spBookDetails", sqlCon)
                    cmd.CommandType = CommandType.StoredProcedure
                    Using dr As SqlDataReader = cmd.ExecuteReader()
                        If dr.HasRows Then
                            rptBookDetails.DataSource = dr
                        End If
                    End Using
                End Using
            End Using
        End Try
    End Sub

 Now over to you:
"A blog is nothing without reader's feedback and comments. So please provide your valuable feedback so that i can make this blog better and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linkedin and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates." 
Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..