解决GridView 单击行改变颜色
很久没用GridView了,上次看到有位同事搞出来,后来问过来的代码忘了,今天整理下以前弄的东西,并借助网络,终于解决了,
以前错误的写法,单击其他行颜色不改变
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "this.style.backgroundColor='#99cc00'; this.style.color='buttontext';this.style.cursor='default';");
}
}
正确的写法如下:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "if(window.oldtr!=null){window.oldtr.runtimeStyle.cssText='';}this.runtimeStyle.cssText='background-color:#99cc00';window.oldtr=this");
}
}
效果如下:
原创文章转载请注明出处:云飞扬IT的blog





