代码改变世界

用JS实现GridView背景颜交替、点击行变色

2010-07-14 16:59  海蓓娜楽  阅读(416)  评论(0编辑  收藏  举报

第一步:在***.CS

protected void gvTestList_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";
          //单击/双击 事件
           string cid = e.Row.RowIndex < 8 ? "0" + (e.Row.RowIndex + 2).ToString() : (e.Row.RowIndex + 2).ToString();
          e.Row.Attributes.Add("OnClick", "ClickEvent('" + cid + "')");
          //注:OnClick参数是指明为鼠标单击时间,后个是调用javascript的ClickEvent函数e.Row.Cells[5].Text
        }
    }

 第二步:在后台(HTML代码区)

    <script language="javascript" type="text/javascript">

    function ClickEvent(cId)
        {
        //调用LinkButton的单击事件,btnBindData是LinkButton的ID
        //        var num=cId;
        //            document.getElementById('ctl00_ContentPlaceHolder1_gvUser_ctl'+num+'_lbtnBindData').click();

        }
    </script>