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);
}
}
Currently rated 4.50 by 6 people
Rate Now!
Date Posted:
06 May 2007 20:50
Last Updated:
Posted by:
Mikesdotnetting
Total Views to date:
13224



Comments
16 August 2009 05:17 from Scott
What is the advantage of this method over just using grow.Attributes.Add("onmouseover","highlight(this,true);"); ?
17 August 2009 07:30 from Mikesdotnetting
@Scott
In some browsers, unless you reset the colour using the onmouseout event, it doesn't change back.