Click here to Skip to main content
13,287,658 members (45,654 online)
Click here to Skip to main content
Add your own
alternative version


51 bookmarked
Posted 10 Jun 2011

Click/Select row in ASP.NET GridView or HTML 5 Table

, 15 Feb 2015
Rate this:
Please Sign up or sign in to vote.
Make entire table row clickable/selectable by adding "onclick" event and formatting features via JavaScript and CSS3.


The suggested technique applies to ASP.NET GridView objects, and essentially to any HTML Table tr element. First, it makes the entire GridView row object (rendered as "tr" element) "clickable" by adding the onclick event (see Listing 1 in C#). The associated event handler procedure performs the custom formatting of the clicked/selected row to make it visually different from the others in the same table (see Listing 2 for JavaScript code snippet implementing this feature).


Click on the image below to open the functional demo of the embedded YouTube video player, demonstrating the suggested technique:

YouTube Embedded Video

Fig 1. Demo snapshot demonstrates the table row corresponding to item 6 has been selected by modifying text attributes in the first cell.

Listing 1: Add onclick event (C# code behind)

protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e){
    if (e.Row.RowType == DataControlRowType.DataRow){
        // javascript function to call on row-click event
        e.Row.Attributes.Add("onClick", "javascript:void SelectRow(this);");

Listing 2: Add JavaScript function to the page head section

<script type="text/javascript">
// format current row
function SelectRow(row) {
 var _selectColor = "#303030";
 var _normalColor = "#909090";
 var _selectFontSize = "3em";
 var _normalFontSize = "2em";
 // get all data rows - siblings to current
 var _rows = row.parentNode.childNodes;
 // deselect all data rows
 try {
     for (i = 0; i < _rows.length; i++) {
         var _firstCell = _rows[i].getElementsByTagName("td")[0]; = _normalColor; = _normalFontSize; = "normal";
 catch (e) { }
 // select current row (formatting applied to first cell)
 var _selectedRowFirstCell = row.getElementsByTagName("td")[0]; = _selectColor; = _selectFontSize; = "bold";

Development Notes

Note 1: Event-binding could also be done on the client side, for example, using jQuery .click() event. For mostly didactic purposes, it's implemented here using C# and the DataRowBound event to be consistent with core .NET technology set.

Note 2: In order to select/play the first item in the grid on "page load" event, use the jQuery code snippet shown below (it inserts a small time delay):

$(document).ready(function () { 
  // start the first item after small delay 
  setTimeout('PlayRowItem()', _initDelay); 

Note 3: As it's been pointed out in the comments, visual effects could be applied to the selected row by using a pair of addClass() and removeClass() jQuery methods. Styling the rows via CSS3 and jQuery rather than directly modifying elements' properties via jQuery as shown in Listing 2. should be considered a preferable approach for future web development, but be aware that certain deficiencies have been reported in regards to these methods prior to jQuery version 1.4.2 (starting from that version and going forward, both methods works seemingly well in all major web browsers).

Additional Resources

GridView/Table rows alternate formatting technique is described here: Alternate Rows in HTML5 Tables GridView [^].

Embedded YouTube SDK is available on CodeProject at:

  1. YouTube™ API for ASP.NET[^]
  2. YouTube™ Embedded Video Player: Extended API (C#)[^]


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Founder Infosoft International Inc
United States United States
Dr. Alexander Bell is a seasoned full-stack Software Engineer (Win/Web/Mobile). He holds PhD in EE/IT, authored 37 inventions and published 300+ technical articles. Currently focused on Java/Javascript, .NET/C#, Android/Mobile, SQL, 'Big Data', Machine Learning, AI, IoT. Alex participated in App Innovation Contests (AIC 2102/2013) with multiple winning submissions. Portfolio samples:

  1. Real-time NY Bus Tracking Web App (IoT)
  2. Android Fraction Calculator "Fractal MK-12"
  3. Android Prime Factorization Calculator 18-digit)
  4. Semaphon™ semantic phone num-to-text converter
  5. Educational Web Portal
  6. Free Online NY Payroll Tax Calculator
  7. WebTV powered by YouTube Player powered by .NET API (#1 on Google)
  8. Engineering Calculator VOLTA
  9. Big Data: Facebook and YouTube
  10. NY City Trivia Quiz
  11. Publications by A. Bell

You may also be interested in...

Comments and Discussions

Generalthanks but i want to select a row on page load or grid view ... Pin
deepakdudeja19-Sep-11 22:04
memberdeepakdudeja19-Sep-11 22:04 
GeneralRe: You are welcome. You could use jQuery "$(document).ready" to... Pin
DrABELL20-Sep-11 4:42
memberDrABELL20-Sep-11 4:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171207.1 | Last Updated 15 Feb 2015
Article Copyright 2011 by DrABELL
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid