代码改变世界

GrideView选中和悬浮效果

2010-01-23 19:35  三皮开发时  阅读(312)  评论(0编辑  收藏  举报

深色的是单击鼠标选中的,浅一点的是鼠标悬停在上面的,具体是:鼠标停在某行上是,该行自动变成上图所示的浅一些的背景色,单击的话,背景色变深,上次单击选中的行背景色恢复最初。

下面是代码:

protected void GvSelectResult_RowCreated(object sender, GridViewRowEventArgs e)

    {

        if (e.Row.RowType == DataControlRowType.DataRow)

        {

            e.Row.Attributes.Add("onmouseover", "if(this.style.backgroundColor!='#b9cddd'&&this.style.backgroundColor!='rgb(185, 205, 221)'){window.trBgcolor=this.style.backgroundColor;this.style.backgroundColor='#CEE3F8'}");

            e.Row.Attributes.Add("onmouseout", "if(this.style.backgroundColor!='#b9cddd'&&this.style.backgroundColor!='rgb(185, 205, 221)')this.style.backgroundColor=window.trBgcolor;");

            e.Row.Attributes.Add("onclick", "if(window.selectedTr!=null){window.selectedTr.style.backgroundColor=window.trSelectedBgcolor;};window.selectedTr=this;window.trSelectedBgcolor=window.trBgcolor;" +

                "this.style.backgroundColor='#b9cddd'");

            //"var trs = this.parentNode.getElementsByTagName('tr');"+

            //    "for( i=1;i<trs.length;i++ )trs[i].style.backgroundColor='#ECF5FF';" +

        }

    }

说明,此代码全面兼容IE和FF。rgb(185, 205, 221)是#b9cddd在FF下的值,也就是上面深背景的颜色(鼠标单击后背景颜色),可以根据需要进行替换。#ECF5FF是上面浅一些的颜色(鼠标悬停时背景颜色),也可以根据需要替换。注意rgb(185, 205, 221)中间有空格,“,”后面有空格,不能去掉,否则FF下效果失效。用到了js全局变量window.selectedTr,window.trSelectedBgcolor,window.trSelectedBgcolor,有冲突时注意换变量名。

------转 吾心飞扬--http://hi.baidu.com/daijun2007/blog/item/d9dcfa111aadcc76ca80c491.html