你要 遍历 它的列
foreach(DataGrid item in grd.items){ 然后
for(int i=0;i<=grd.coloumns.count-1;i++)
{
e.Item.cells[i].Attributes.Add("onmouseover", "currentcolor=this. style.backgroundColor; this.style.backgroundColor = '#faf0e6' ");
e.Item.cells[i].Attributes.Add("onmouseout", "this.style.backgroundColor = currentcolor");
}
}
foreach(DataGrid item in grd.items){ 然后
for(int i=0;i<=grd.coloumns.count-1;i++)
{
e.Item.cells[i].Attributes.Add("onmouseover", "currentcolor=this. style.backgroundColor; this.style.backgroundColor = '#faf0e6' ");
e.Item.cells[i].Attributes.Add("onmouseout", "this.style.backgroundColor = currentcolor");
}
}
#region DataGrid Item Create
public void DataGrid_ItemCreate(objectsender,System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
{
e.Item.Attributes.Add("onclick","javascript:return CG(this);");//单击改变颜色
}
}
#endregion
客户端代码:(javascript):
<script language=javascript>function CG(obj)
{
var Curr_TR=document.all.tags("tr");
for(var i=1;i<Curr_TR.length;i++)
{
Curr_TR[i].Style.backgroundColor="white";
}
obj.style.backgroundColor="yellow";
}
</script>
{
e.Item.Attributes.Add("onmouseover","this.oldColor=style.backgroundColor;style.backgroundColor='#FFFFCC'");
e.Item.Attributes.Add("onmouseout","style.backgroundColor=this.oldColor");
}
Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
e.Item.Attributes.Item("onclick") = "DoTrClick(this)"
End If
End Sub
客户端代码:
var lastObj,lastColor;
function doTrClick(objTr) {
if(lastObj==undefined)
{
lastObj = objTr;
}
lastObj.style.backgroundColor = lastColor;
lastObj = objTr;
lastColor=lastObj.style.backgroundColor;
lastObj.style.backgroundColor = "#CFF3D5"; // 设置选中行背景色
}
前台JS代码
function setItemBackColor(obj)
{
var Curr_TR=document.all.tags("tr");
for(var i=1;i<Curr_TR.length;i++)
{
if (Curr_TR[i].style.backgroundColor =="yellow")
{
Curr_TR[i].style.backgroundColor="white";
}
}
obj.style.backgroundColor="yellow";
}
后台C#代码
private void dgDocClass_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
e.Item.Attributes.Add("onclick","return setItemBackColor(this)");
}
}
马上结贴,得分啦!
e.Item.Attributes.Add("onmouseout", "style.backgroundColor = getAttribute('attrb');");
//2、但是下面的代码,单击鼠标可以实现更改。但并不能更改失去焦点的颜色。e.Item.Attributes.Add("onclick", "setAttribute('attrb', style.backgroundColor); style.backgroundColor = '#faf0e6' ");
e.Item.Attributes.Add("onblur", "style.backgroundColor = getAttribute('attrb');");
e.Item.Attributes.Add("ondeactivate", "runtimeStyle.backgroundColor=''");
的办法好用!
有blur的一般是input,textarea,button,select等东西。