DataGrid单击一行改变此行背景色
1) 将DataGrid控件的AlternatingItemStyle.BackColor属性设置为“Beige”。
2) 在DataGrid控件创建项时激发的事件(DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e))中添加如下代码:
private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if ( e.Item.ItemIndex > -1 )
{
e.Item.Attributes["name"] = "SelectTR";
e.Item.Attributes["onclick"] = "SelectNode(this)";
}
}
3) 在HTML代码页中添加JavaScript代码如下:
function SelectNode(objThis)
{
var objTrs = document.getElementsByTagName("TR");
var j = 0;
for(var i = 0; i < objTrs.length - 1; i = i + 2 )
{
j = i + 1;
if(objTrs[i].name == "SelectTR" )
objTrs[i].style.backgroundColor = "Beige";
if ( objTrs[j].name == "SelectTR" )
objTrs[j].style.backgroundColor = "#ffffff";
}
objThis.style.backgroundColor = "#c6ff00";
}
大家不妨试一下!^_^