怎么设置当鼠标点击GridView某一行时,那么这行显示底色为黄色,当鼠标点击GridView另一行时,上次点击的那行底色还原为以前的颜色,被点击的行底色设置为黄色.

解决方案 »

  1.   

    RowEnter    RowLeave这两个事件
      

  2.   

    假设原来是white色,在RowDataBound事件里写:
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    e.Row.Attributes["onclick"] = "var rows=this.parentNode.rows;for(var i=0;i<rows.length;i++)rows[i].style.background='white';this.style.background='yellow';";
    }
      

  3.   

    RowClick吧。
    CurrentRow.BackColor
      

  4.   

    <script>
    var oldClr;
    function chgClr(obj){
      var rows;
      oldClr=obj.style.backgroundColor;
      rows=this.parentNode.rows;
      for(var i=0;i<rows.length;i++){ 
          rows[i].style.backgroundColor=oldClr;
          obj.style.backgroundColor='yellow';
      }
    }
    </script>protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       e.Row.Attributes.Add["onclick","chgClr(this)"];  
    }
      

  5.   

    完整的代码
    http://dotnet.aspx.cc/file/Change-GridView-Row-Background-Color-When-Click-Row.aspx
      

  6.   

    +1 收藏了。 顺便问一下, 不点击的情况下 背景色能设置么? 就像winform下datagridview的行背景色 当我某一列值为1时 背景色为红色  为2时背景色为绿色在winform下我会 在datagridview中的databding事件中可以写 不知道asp.net下有没有这么做的。。
      

  7.   

    protected void gvShowPlanInfor_RowDataBound(object sender, GridViewRowEventArgs e)
        {
     
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // 给单元格增加鼠标经过时指针样式
                e.Row.Attributes["style"] += "cursor:pointer;cursor:hand;";
                ViewState["GVCurrentRow"] = e.Row.RowIndex;
                int intRowIndex = e.Row.RowIndex;
                //intRowIndex = intRowIndex;
                //当鼠标停留时更改背景色
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#9DA2F7'");
                //当鼠标移开时还原背景色
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");            e.Row.Attributes.Add("onClick", "javascript:__doPostBack('" + gvShowPlanInfor.ID + "','Select$" + intRowIndex + "');");
            } 
        }
      

  8.   

    非常感谢各位,点数据单元格时,数据行颜色切换可以实现.但点前面的选择按钮那么页面会刷新,数据行颜色会丢失。点前面的选择按钮(因想获取GridView单元格的内容)