Hilight a HTMLTable row when selected

Posted on 2013-06-03
Last Modified: 2013-06-24
How can I Hilight a HTMLTable row when selected?   I know how to change the color, and I have code to find the row by ID, but how/where can I define handle a click event?   I want to do this in the codebehind

This code hilights a row:

           ctl = CType(Me.Page.FindControlRecursively("row"), HtmlTableRow)
            If Not IsNothing(ctl) Then

                 ' This will be used for each cell on the row.
                Dim recordRowCell As System.Web.UI.HtmlControls.HtmlTableCell

                ' Set the background color on the record row in RGB format.
                Dim color As String = "#fff000"

                ' For each cell, set the background color.
                For Each recordRowCell In ctl.Cells

                    ' Override the record row background color -- since each data cell uses a
                    ' style (by default: table_cell or table_cellr)
                    recordRowCell.Style.Add("background-color", color)


            End If
Question by:HLRosenberger
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 36

Expert Comment

by:Miguel Oz
ID: 39217635
You do not need code behind use css to add mouse hover effect on each row as follows:
.mytable tr:hover,  selectedrow
  background-color: #632a2a;
  color: #fff;

Open in new window

I assume your table markup is like:
<table class="mytable">
                   Your table contents goes here

Open in new window

Note: Class mytable is only used to identify which table requires the css rule.

I f you need to do this when user clicks the row, you can add Javascript onclick event and define a method  that remove the class selectedrow from the selected row markup and added to the current row defined by the click event. Let me know if you want to go that path to provide the code and if you are using jquery. (VS2010 and VS2012 add this JS library by default to web projects)

Author Comment

ID: 39226751
I do not want the change on hover over.  I want to change the color on click.  Yes, please provide the javascript for this.  Thanks!
LVL 36

Accepted Solution

Miguel Oz earned 500 total points
ID: 39233686
OK, this method in Jquery setup all click event on cells as follows:
function SetupClientClick() {
$("TD").click(function() {
  $(this).parents("tr").toggleClass("selectedrow", this.clicked);

Open in new window

  background-color: #632a2a;
  color: #fff;

Open in new window

You can call it once your document is ready or as a startup script (see example first link)

For further info, check:
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 25

Expert Comment

ID: 39238086
I wrote this today, is it what you're looking for?  Client-side click?

Author Comment

ID: 39248900
I do not know jQuery.

Author Closing Comment

ID: 39271347

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Name Space error VS2015 1 36
Running powershell scripts from ASP.NET 6 57
Need help with a query 14 37
CSS for Popup in ASP.NET 4 16
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below.…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question