62,269
社区成员
发帖
与我相关
我的任务
分享
protected void SmartGridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
int i;
//执行循环,保证每条数据都可以更新
for (i = -1; i < SmartGridView1.Rows.Count; i++)
{
//首先判断是否是数据行
if (e.Row.RowType == DataControlRowType.DataRow)
{
//当鼠标停留时更改背景色
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");
//当鼠标移开时还原背景色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
}
}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<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==""?colorHighlt:"");
}
var lblId = "<%=Lbl1.ClientID %>";
var lbl = $e(lblId);
lbl.innerText = row.cells(0).innerText;//Label更新为当前行第一列的值
oldRow = row;
}
function $e(id)
{
return document.getElementById(id);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Lbl1" runat="server" Text="初始值"></asp:Label>
<asp:GridView runat="server" ID="GridView1" Width="50%">
</asp:GridView>
</div>
</form>
</body>
</html><script language="javascript" type="text/javascript" >
var prev=null;
function selectx(row) /**//*改变选中行的颜色还原为选中行的颜色*/
{
if(prev!=null)
{
prev.style.backgroundColor='#E4F7D8';
}
row.style.backgroundColor='#8EC26F';
prev=row;
}
</script>