前台方式:
<script language="javascript" type="text/javascript">
function SetBGC()
{
var style;
var obj = document.getElementById("lv_roleList").getElementsByTagName("tr");
for(var i=0; i<obj.length; i++){
obj[i].onclick=function(){
style = this.style.background; //记录当前样式 (背景)
for(var j=0; j<obj.length; j++){
obj[j].style.background = ""; //将所有行的样式清空
}
this.style.background = style; //还原当前行样式
this.style.background = this.style.background == "#ff0000" ? "" : "#ff0000"; //调整将当前行样式
}
}
}
if(window.attachEvent)
window.attachEvent("onload",SetBGC);
</script>
后台方式:
protected override void Render(HtmlTextWriter writer)
{
foreach (GridViewRow row in this.lv_roleList.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
row.Attributes["onclick"] = ClientScript.GetPostBackEventReference(this.lv_roleList, "Select$" + row.RowIndex.ToString(), true);
row.Attributes["style"] = "cursor:pointer";
row.Attributes["title"] = "单击选择行";
row.Attributes.Add("onclick", "if(window.oldtr!=null){window.oldtr.runtimeStyle.cssText='';}this.runtimeStyle.cssText='background-color:#99cc00';window.oldtr=this");
}
}
base.Render(writer);
}
从别处摘来的,好不好用还没试过,暂时留做收藏,以备后用