Freakishly Freakishly - 1 month ago 4
Javascript Question

How can I add an onclick event to just the first column of a gridview and not the entire row?

Title is pretty self-explanatory. I have a gridview, and when the user clicks on a particular row, I add a shadow to the first column. The problem is, the user can click anywhere on the row, and the shadow is added, but the link is only opened in a new tab if the user clicks on the link (i.e. first column). How do I add the onclick event to the first column of every row only?

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


If it helps, I used the example here to implement SelectRow.

Answer

Add it to e.Row.Cells[0] instead

if (e.Row.RowType == DataControlRowType.DataRow)
{
   e.Row.Cells[0].Attributes.Add("onClick", "SelectRow(this);");
}
Comments