Monday, December 1, 2008

关于GridView数据行的鼠标悬停高亮的问题

其实这个问题很简单,只要两行代码就可以解决。

private void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
e.Item.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#66CCFF'");
e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
}
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#6699ff';");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor;");
}
}

No comments: