Solved

Hilight a HTMLTable row when selected

Posted on 2013-06-03
6
233 Views
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)

                Next

            End If
0
Comment
Question by:HLRosenberger
  • 3
  • 2
6 Comments
 
LVL 35

Expert Comment

by:Miguel Oz
Comment Utility
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)
0
 
LVL 1

Author Comment

by:HLRosenberger
Comment Utility
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!
0
 
LVL 35

Accepted Solution

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

Open in new window

where
.selectedrow
{ 
  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:
http://api.jquery.com/click/
http://stackoverflow.com/questions/1287699/jquery-highlight-de-highlight-table-row-on-click
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 25

Expert Comment

by:Rouchie
Comment Utility
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
0
 
LVL 1

Author Comment

by:HLRosenberger
Comment Utility
I do not know jQuery.
0
 
LVL 1

Author Closing Comment

by:HLRosenberger
Comment Utility
thanks,.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now