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: 87355


- 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

Ghazanfar 30/01/2016 06:43
In response to Getting Started with ASP.NET MVC 5 using Visual Basic
Nice working. Please keep it up to convert csharp code into Its very helpful for vb...

sara 29/01/2016 09:39
In response to Simple Login and Redirect for ASP.NET and Access
Hi there, I am trying to validate and check for inputs entered. When I don't enter any inputs and...

Martin Thatcher 28/01/2016 17:28
In response to MVC 5 with EF 6 in Visual Basic - Advanced Entity Framework Scenarios
A small typo I think. In the code section that begins Function Index(ByVal SelectedDepartment As As...

Suresh_thefame 28/01/2016 08:03
In response to Sessions and Shopping Carts

Andrey Kurdyumov 28/01/2016 05:47
In response to ASP.NET 5: Uploading files with ASP.NET MVC 6
@Lee IFormFile has OpenReadStream(): Stream method...

Alisa 27/01/2016 18:37
In response to A Better Way To Export Gridviews To Excel
Thanks for this example! I am also asking about the date fields having to be re-formatted when the a...

Dan Buckley 27/01/2016 00:47
In response to What ASP.NET Can And Cannot Do
New to all things programming, this was very helpful and clear. Please write more....

satyabrata 26/01/2016 16:16
In response to Request.Form Is Empty When Posting To ASPX Page
Thank you....

david sanchez 26/01/2016 09:51
In response to ASP.NET MVC 5 with EF 6 - Working With Files
Thanks! This line " <img src="~/images/@Model.FilePaths.First(f => f.FileType == alt="" />" an...

Bryon 25/01/2016 15:06
In response to Windows Authentication With ASP.NET Web Pages
Was hoping this would help solve the issues I'm having. Then I saw the dates and new it was too old....