GridView 样式如下: <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White"
BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="Id"
Font-Size="10pt" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating"
PageSize="20">
<RowStyle BackColor="White" ForeColor="#330099" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" CssClass="Freezing" />
<AlternatingRowStyle BackColor="#f5f5f5"/>
</asp:GridView>如何实现鼠标每单击一行,该行变色,再单击变回原来颜色(注:行颜色是一白一灰的),请大家帮忙看看。。
BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="Id"
Font-Size="10pt" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating"
PageSize="20">
<RowStyle BackColor="White" ForeColor="#330099" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" CssClass="Freezing" />
<AlternatingRowStyle BackColor="#f5f5f5"/>
</asp:GridView>如何实现鼠标每单击一行,该行变色,再单击变回原来颜色(注:行颜色是一白一灰的),请大家帮忙看看。。
{
if(id.style.backgroundColor=="#CCC")
id.style.backgroundColor=white;
else
id.style.backgroundColor="#CCC";
}
为GridView的行添加Onclick事件"chbg(this)"
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "ItemOver(this)");
}
}js代码:
<script language="javascript" type="text/javascript">
if (!objbeforeItem)
{
var objbeforeItem=null;
var objbeforeItembackgroundColor=null;
}
function ItemOver(obj)
{
if(objbeforeItem)
{
objbeforeItem.style.backgroundColor = objbeforeItembackgroundColor;
}
objbeforeItembackgroundColor = obj.style.backgroundColor;
objbeforeItem = obj;
obj.style.backgroundColor = "#FFFF00";
}
</script>
<script type="text/javascript">
var gdv = '<%=GridView1.ClientID%>';
function ChangeBg()
{
var d;
var obj = document.getElementById(gdv).getElementsByTagName("tr");
for(var i=0; i<obj.length; i++)
{
obj[i].onclick=function()
{
d = d == "#fffddf" ? "#ffffff" : "#fffddf";
this.style.backgroundColor = d;
}
}
} if(window.attachEvent)
window.attachEvent("onload",ChangeBg);
</script>
<body>
<form runat="server">
这里放GridView控件,假设ID是GridView1
</form>
</body>
</html>散分走人
<head runat="server">
<title>无标题页</title>
<script language="javascript" type="text/javascript">
var oldRow = null;
window.onload=function()
{
var tblId = "<%=GridView1.ClientID %>";
var tbl = $e(tblId);
if(tbl!=null)
{
for(var i=1;i<tbl.rows.length;i++)
{
tbl.rows[i].onclick=new Function("clickRow(this)")
}
}
}
function clickRow(row)
{
var colorHighlt = "red";
//alert(row.rowIndex);
if(oldRow!=row)
{
if(oldRow!=null)
{
oldRow.style.backgroundColor = "";
}
row.style.backgroundColor = colorHighlt;
}else
{
row.style.backgroundColor=(row.style.backgroundColor==""?"red":"");
}
oldRow = row;
}
function $e(id)
{
return document.getElementById(id);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView runat="server" ID="GridView1" Width="50%">
</asp:GridView>
</div>
</form>
</body>
</html>
在GridView的RowDataBound事件中输入以下代码:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "ItemOver(this)");
}
} js:
<script type="text/javascript">
function ItemOver(obj)
{
obj.style.backgroundColor = (obj.style.backgroundColor==""?"#FFFF00":"")
}
</script>
{
//改变选中行的颜色
var obj = document.getElementById("MyDataGrid1");
if ( obj != null )
{
for ( var i = 1 ;i < obj.rows.length-1;i=i+2)
{
obj.rows[i].className = "dgItemStyle";
obj.rows[i+1].className = "dgAlternatingItemStyle";
}
}
tr.className="dgSelectedItemStyle";
} 在RowDataBind() 事件里 加上这个rowClick 就OK 了
{
if(id.style.backgroundColor=="#CCC")
id.style.backgroundColor=white;
else
id.style.backgroundColor="#CCC";
}
为GridView的行添加Onclick事件"chbg
#region "定义移动鼠标变色代码"
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#ACA899'");
//当鼠标移开时还原背景色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
}
}
#endregion
例如:
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
用C#实现 无语宝马耕田咯
{
int x,y;
x = dataGridView1.CurrentRow.Index;
for (y = 0; y <= dataGridView1.Rows[x].Cells.Count-1; y++)
{
if (dataGridView1.Rows[x].Cells[y].Style.BackColor == System.Drawing.Color.White)
{
dataGridView1.Rows[x].Cells[y].Style.BackColor = System.Drawing.Color.Gray;
}
else
{
dataGridView1.Rows[x].Cells[y].Style.BackColor = System.Drawing.Color.White;
}
} }
呵呵
//GridView事件绑定 (子模块 绑定)
void gvModule_RowDataBound(object sender, GridViewRowEventArgs e)
{
// e.Row.Attributes.Add("onmouseover", "color=this.style.backgroundColor;this.style.backgroundColor='#ADB2C6'");
// e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=color");}
<head runat="server">
<title>无标题页</title>
<script language="javascript" type="text/javascript">
var oldRow = null;
window.onload=function()
{
var tblId = "<%=GridView1.ClientID %>";
var tbl = $e(tblId);
if(tbl!=null)
{
for(var i=1;i<tbl.rows.length;i++)
{
tbl.rows[i].onclick=new Function("clickRow(this)")
}
}
}
function clickRow(row)
{
var colorHighlt = "red";
//alert(row.rowIndex);
if(oldRow!=row)
{
//if(oldRow!=null)
//{
//oldRow.style.backgroundColor = "";//这个地方屏蔽了就 可以了
//}
row.style.backgroundColor = colorHighlt;
}else
{
row.style.backgroundColor=(row.style.backgroundColor==""?"red":"");
}
oldRow = row;
}
function $e(id)
{
return document.getElementById(id);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView runat="server" ID="GridView1" Width="50%">
</asp:GridView>
</div>
</form>
</body>
</html>
<html> <head> <title> </title> </head>
<script type="text/javascript">
var gdv = ' <%=GridView1.ClientID%>';
function ChangeBg()
{
var d;
var obj = document.getElementById(gdv).getElementsByTagName("tr");
for(var i=0; i <obj.length; i++)
{
obj[i].onclick=function()
{
d = d == "#cccccc" ? "#ffffff" : "#cccccc"; //我不知道你使用的是什么灰色。。
this.style.backgroundColor = d;
}
}
} if(window.attachEvent)
window.attachEvent("onload",ChangeBg);
</script>
<body>
<form runat="server">
这里放GridView控件,假设ID是GridView1
</form>
</body>
</html>
<style type="text/css">
tr{ryo:expression(onclick=function(){this.style.backgroundColor=(this.style.backgroundColor=='#cccccc'?((this.rowIndex%2==0)?"whitesmoke":"white"):'#cccccc')})}
</style>
var NewRowBgColor=""; //鼠标滑过的颜色
var MarkRowBgColor="yellow"; //鼠标点下的颜色
function ChangeRowBg(row)
{
if(event.type== 'mouseover')
{
OldRowBgColor = row.style.backgroundColor
row.style.backgroundColor = NewRowBgColor
}
else if (event.type== 'mouseout')
{
row.style.backgroundColor = OldRowBgColor;
}
}
function ChangeRowBgEven(row)
{
if (event.type== 'mousedown')
{
if (row.style.backgroundColor != MarkRowBgColor)
{
row.style.backgroundColor = MarkRowBgColor;
row.onmouseout = function(){return false;}
row.onmouseover= function(){return false;}
}
else
{
row.style.backgroundColor= '';
}
}
}
function ChangeRowBgImpair(row)
{
if (event.type== 'mousedown')
{
if (row.style.backgroundColor!= MarkRowBgColor)
{
row.style.backgroundColor = MarkRowBgColor;
row.onmouseout = function(){return false;}
row.onmouseover= function(){return false;}
}
else
{
row.style.backgroundColor = NewRowBgColor;
}
}
} 后台:
private static void ChangeRowBg(GridView grdTable)
{
for (int i = 0; i < grdTable.Rows.Count; i++)
{
grdTable.Rows[i].Attributes.Add("onmouseover ", "ChangeRowBg(this) ");
grdTable.Rows[i].Attributes.Add("onmouseout ", "ChangeRowBg(this) ");
if (i % 2 == 0)
grdTable.Rows[i].Attributes.Add("onmousedown ", "ChangeRowBgEven(this) ");
else
grdTable.Rows[i].Attributes.Add("onmousedown ", "ChangeRowBgImpair(this) ");
}
}
这样调用: ChangeRowBg(this.rscountGridView);
ChangeRowBg(this.rsredGridView);
谢谢Cutbug,这是我之前希望达到的一个效果.