鼠标停留在GridView某一行时,行的颜色改变

protected void gvdegreetype_RowDataBound(object sender, GridViewRowEventArgs e)
       {
           if (e.Row.RowType == DataControlRowType.DataRow)
           {
               e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='GhostWhite'");
               e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='white'");
           }
       }

或:

1.增加GridViewGVSelect_RowDataBound事件
protectedvoid GVSelect_RowDataBound(object sender, GridViewRowEventArgs e)
      {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //当鼠标停留时更改背景色
            e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#8EC26F'");
            //当鼠标移开时还原背景色
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            //设置悬浮鼠标指针形状为"小手"
            e.Row.Attributes["style"] = "Cursor:hand";
 
            //单击/双击 事件
              e.Row.Attributes.Add("OnClick", "ClickEvent('" + e.Row.Cells[0].Text + "')");
            //注:OnClick参数是指明为鼠标单击时间,后个是调用javascriptClickEvent函数
          }
}
2在页面的HTML里添加javascript函数,用来响应鼠标点击事件,因为ASP客户端不能主动调用服务端的函数,我在这里添加一个LinkButton,将其text设置为空,然后在ClickEvent(cId)函数中,调用这个LinkButton的单击事件。
<scriptlanguage="javascript">
    function ClickEvent(cId)
    {
        //调用LinkButton的单击事件,btnBindDataLinkButtonID
        document.getElementById("btnBindData").click();
      }
</script>
3.添加LinkButton的响应事件
   protectedvoid btnBindData_Click(object sender, EventArgs e)
{
         //在这里对你需要的数据信息进行输出
           SetClientInfo();//我的处理函数
}
GridView鼠标停留变色和单击处理事件,当鼠标在GridView的行上停留时,将该行变色,当单击该行时,做相应处理

你可能感兴趣的:(GridView)