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
  • 3
  • 2
LVL 35

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 35

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:
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project

789 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