Link to home
Start Free TrialLog in
Avatar of HLRosenberger
HLRosenbergerFlag for United States of America

asked on

Hilight a HTMLTable row when selected

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)

                Next

            End If
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

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">
          <tr>
                   Your table contents goes here
          </tr>
</table>

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)
Avatar of HLRosenberger

ASKER

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!
ASKER CERTIFIED SOLUTION
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I wrote this today, is it what you're looking for?  Client-side click?
http://www.codepal.co.uk/show/Highlight_a_Gridview_table_row_on_selection_using_JQuery
I do not know jQuery.
thanks,.