How to highlight a GridView's row on hover

As the user hovers over each row, it becomes highlighted, with the help of a small amount of javascript.

The javascript:

    
function highlight(tableRow, active)
{
if (active)
{
tableRow.style.backgroundColor = '#cfc';
}
else
{
tableRow.style.backgroundColor = '#fff';
}
}

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

The Databound event of the GridView:

protected void GridView1_DataBound(object sender, EventArgs e)
{
  foreach (GridViewRow grow in GridView1.Rows)
  {
    grow.Attributes["onmouseover"] = "highlight(this, true);";
    grow.Attributes["onmouseout"] = "highlight(this, false);";
    HttpResponse myHttpResponse = Response;
    HtmlTextWriter myHtmlTextWriter = new HtmlTextWriter(myHttpResponse.Output);
    grow.Attributes.AddAttributes(myHtmlTextWriter);
  }
}

You might also like...