黑马程序员技术交流社区

标题: gridview 行选添加颜色和事件 [打印本页]

作者: 陈君    时间: 2014-9-11 17:02
标题: gridview 行选添加颜色和事件
在gridview 列表中,需要添加如下效果,如果经过时变色,移开时还原成以前的颜色,行选中时通过加深色,从而知道选中了哪行
在gridview 列表中,需要添加如下效果,如果经过时变色,移开时还原成以前的颜色,行选中时通过加深色,从而知道选中了哪行,特别是列表数据比较多的时候,标志很重要,同时触发选中行事件,那么如何 行选中加颜色,同时也出发我们事件了?如果我们通入 e.Row.Attributes.Add("onclick", 绑定两次,发现只能触发其中的一个,其实可以把另个放到一个里面

下面是具体的例子:

  1. protected void EditGridview_RowDataBound(object sender, GridViewRowEventArgs e)
  2. {
  3. if (e.Row.RowType== DataControlRowType.DataRow)
  4. {
  5. //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色
  6. e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='inactivecaptiontext',this.style.fontWeight='';this.style.cursor='hand';");
  7. //当鼠标离开的时候 将背景颜色还原的以前的颜色
  8. e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
  9. //当鼠标单击时,加深色标志
  10. e.Row.Attributes.Add("onclick", "setvalue();if(window.oldtr!=null){window.oldtr.runtimeStyle.cssText='';}this.runtimeStyle.cssText='background-color:#e6c5fc';window.oldtr=this");

  11. }


  12. }
复制代码

js代码:

  1. <script type="text/javascript" >
  2. function setvalue() {
  3. alert('行单击事件,同时变色');
  4. }
  5. </script>
复制代码








欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2