How to make an ASP.NET Table row highlight and become clickable

4.7 (10 votes)

For those people who like to output their data to an asp:Table, highlighting the entire row and making it clickable can make it obvious to the user that some action is expected from them. This emulates the behaviour that can be found in some desktop applications. All that's needed for this is a bit of javascript, and the use of AddAttributes() for the table rows.

You will also need to get round the limitation of the fact that the <a> tag is not allowed around a table row, so here are the two javascript functions that perform the magic:

function highlight(tableRow, active)
if (active)
{ = '#cfc';
{ = '#fff';

function link(Url)
document.location.href = Url;

And here's the method that adds the javascript to each row of the table as it is rendered:

string connstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
connstring += Server.MapPath("App_Data/NWind.mdb");
string query = "SELECT [CustomerID], [CompanyName], [ContactName], [Address], [City]";
query += " FROM [Customers]";
OleDbConnection conn = new OleDbConnection(connstring);
OleDbDataAdapter da = new OleDbDataAdapter(query, conn);
DataSet ds = new DataSet();
da.Fill(ds, "Employees");
foreach (DataRow dr in ds.Tables[0].Rows)
    TableRow trow = new TableRow();
    foreach (DataColumn dc in ds.Tables[0].Columns)
      TableCell tcell = new TableCell();
      tcell.Controls.Add(new LiteralControl(dr[dc.ColumnName].ToString()));
      trow.Attributes["onmouseover"] = "highlight(this, true);";
      trow.Attributes["onmouseout"] = "highlight(this, false);";
      trow.Attributes["onclick"] = "link('edit.aspx?id=" + dr[0].ToString() + "');";
      HttpResponse myHttpResponse = Response;
      HtmlTextWriter myHtmlTextWriter = new HtmlTextWriter(myHttpResponse.Output);

Date Posted:
Last Updated:
Posted by:
Total Views to date: 86507


- James Piette

I recently implemented similar functionality on a site I'm building using ASP MVC. I was able to do this using CSS and jquery. I also was using an AJAX call to have a table in a left division post the data to the rightDiv (division with id="divRight".

After including jquery, put this script on the top of the page.
<script type="text/javascript">
function linkfunc(url) {

Then declare the <tr> tag as follows. (replace the stuff in [] to match your controller call.)
tr class="highlight" onclick="linkfunc('<%= Url.Action("[controlleraction]", "[controllername]", new{/**/})%>');">

To have highlighting work, declare this in the .css file

- Mike


Since I wrote this article over 3 years ago, I have been using jQuery too.

- Mindaugas Matulis

There is a much easier way to do this without javascript. Just when you create Table Row items you can simply change colore on attribute name like in previous example:

trow.Attributes["onmouseover"] = "this.bgColor = '#DCEAF3'";
trow.Attributes["onmouseout"] = "this.bgColor = '#FFFFFF'";

- Mike

@Mindaugas Matulis

Thanks, but you are still using Javascript.

This article is very old. I'd recommend managing this via jQuery or similar these days anyway.

Recent Comments

raphael 27/11/2015 13:59
In response to ASP.NET 5: Managing Client-side Dependencies with NPM, Bower and Gulp
i fixed it. i'm not sure *what* it is that VS does, but you get the same result if you do a "npm on...

Raphael 27/11/2015 13:02
In response to ASP.NET 5: Managing Client-side Dependencies with NPM, Bower and Gulp
There seems to be a major issue with the dependency manager in VS2015. I haven't yet figured out but...

Joaquín Bresan 26/11/2015 17:58
In response to Getting the identity of the most recently added record
Great contribution friend. Thank you so much for sharing....

sumalatha 24/11/2015 22:09
In response to ASP.NET 5 Middleware, Or Where Has My HttpModule Gone?
Great article. I have gone through hundreds of articles, finally this gives the comprehensive of...

Rajasekar 24/11/2015 12:27
In response to Import Data From Excel to Access with ASP.NET
While use this code i'm facing on error: "Unrecognized Database format C:\mydabase.accdb" can any...

Parmod 24/11/2015 07:28
In response to ASP.NET 5 Project Basics
For a new learner (Fresher) in ASP.NET there is a issue Fresher have to learn two types of , old...

Robert 22/11/2015 21:35
In response to ASP.NET 5 By Numbers
I have to agree fully with Paul, this does sound like an entire mis-mash of technologies. Sort of in...

Christian 21/11/2015 15:46
In response to MVC 5 with EF 6 in Visual Basic - Creating an Entity Framework Data Model
Many thanks Mike to introduce me in the EF6 Code First way of thinking. Exactly what I need for my...

ax plains 20/11/2015 16:29
In response to Examining the Details and Delete Methods
Hello, really great tutorial for a beginner like me! Is it possible to have an explanation on how a...

Abdul Latif 20/11/2015 14:42
In response to Reading Excel Files Without Saving To Disk In ASP.NET
Could anyone please help me, I am getting : "OfficeOpenXml.ExcelPackage" does not contain a for...