ITPUB??ì3
ITPUB论坛 » WEB 2.0技术 » 用javascript/css实现GridView行背景色交替、点击行变色


标题: 用javascript/css实现GridView行背景色交替、点击行变色
本帖已经被作者加入个人空间
离线 银河使者


精华贴数 0
个人空间 7579
技术积分 477 (3978)
社区积分 1 (43896)
注册日期 2008-2-8
论坛徽章:1
现任管理团队成员     
      

发表于 2008-5-19 22:41 
用javascript/css实现GridView行背景色交替、点击行变色

前几天在博问里发现有人问关于 GridView点击行变色 的问题,突然想起很久很久以前,写过一篇文章 一个简单但常用的表格样式--鼠标划过行变色--简洁实现 ,是关于表格行颜色交替和鼠标指向时变色的,正好今天把那一篇补充和扩展一下,加上鼠标点击选择(其实只是点击后变个颜色,“选择”这个词在这里不合适),顺便把这个直接应用到GridView上,如果是其他的控件,或者直接的HTML,稍加修改也可以用上,这里仅提供一个思路。虽然GridView 使用AlternatingRowStyle提供了交替行背景色的问题,但这个东西用着实在不爽,看它生成到HTML的那个table,那叫一个乱啊。

    下面是代码,注释应该还算比较详细,比较适合初学者,可以把下面两个文件的代码直接复制到你的项目中直接执行。最下面有文件的下载地址,也可以直接下载后运行,代码在IE7和Firefox2下测试通过,有任何问题,请在下面留言,我将尽量及时回复。

BackgroundColor.aspx
主要包含一个GridView,是我们折腾的重点对象,还有一堆javascript,是我们折腾GridView的手段
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="BackgroundColor.aspx.cs" Inherits="_BackgroundColor" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>BackgroundColor</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:GridView runat="server" ID="gvMeiMingZi"></asp:GridView>
    </form>
    <script type="text/javascript">
        //把事件放在onload里,因为我不知道JS如果直接写到这儿是不是会等页面加载完才执行
        //使用<%=%>方式输出GridView的ID是因为某些情况下(如使用了MasterPage)会造成HTML中ID的变化
        //颜色值推荐使用Hex,如 #f00 或 #ff0000
        window.onload = function(){
            GridViewColor("<%=gvMeiMingZi.ClientID%>","#fff","#eee","#6df","#fd6");
        }
      
        //参数依次为(后两个如果指定为空值,则不会发生相应的事件):
        //GridView ID, 正常行背景色,交替行背景色,鼠标指向行背景色,鼠标点击后背景色
        function GridViewColor(GridViewId, NormalColor, AlterColor, HoverColor, SelectColor){
            //获取所有要控制的行
            var AllRows = document.getElementById(GridViewId).getElementsByTagName("tr");
           
            //设置每一行的背景色和事件,循环从1开始而非0,可以避开表头那一行
            for(var i=1; i<AllRows.length; i++){
                //设定本行默认的背景色
                AllRows.style.background = i%2==0?NormalColor:AlterColor;
               
                //如果指定了鼠标指向的背景色,则添加onmouseover/onmouseout事件
                //处于选中状态的行发生这两个事件时不改变颜色
                if(HoverColor != ""){
                    AllRows.onmouseover = function(){if(!this.selected)this.style.background = HoverColor;}
                    if(i%2 == 0){
                        AllRows.onmouseout = function(){if(!this.selected)this.style.background = NormalColor;}
                    }
                    else{
                        AllRows.onmouseout = function(){if(!this.selected)this.style.background = AlterColor;}
                    }
                }

                //如果指定了鼠标点击的背景色,则添加onclick事件
                //在事件响应中修改被点击行的选中状态
                if(SelectColor != ""){
                    AllRows.onclick = function(){
                        this.style.background = this.style.background==SelectColor?HoverColor:SelectColor;
                        this.selected = !this.selected;
                    }
                }
            }
        }
    </script>
</body>
</html>

BackgroundColor.aspx.cs
用于生成一堆用于测试的数据,不然前面的GridView里啥也没有,就看不出效果了
using System;
using System.Data;

public partial class _BackgroundColor:System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //生成DataTable并添加10个列
        DataTable dt = new DataTable();
        for(int i = 0; i < 10; i++)
        {
            dt.Columns.Add();
        }

        //往DataTable里添加20行数据
        for(int i = 0; i < 20; i++)
        {
            dt.Rows.Add(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
        }

        //将DataTable绑定到GridView
        gvMeiMingZi.DataSource = dt;
        gvMeiMingZi.DataBind();
    }
}


只看该作者    顶部
离线 mosticy
中级会员


精华贴数 0
个人空间 0
技术积分 2380 (624)
社区积分 2182 (511)
注册日期 2007-10-9
论坛徽章:2
      
      

发表于 2008-5-20 14:43 
不需要这么复杂。


__________________
(止步海阔天空.)
只看该作者    顶部
离线 zyzdy
鲫鱼煮稀饭


精华贴数 12
个人空间 0
技术积分 15826 (66)
社区积分 3770 (331)
注册日期 2001-12-11
论坛徽章:30
现任管理团队成员     
      

发表于 2008-5-25 16:34 
定义二个不同的class
PHP code:


<c:choose>

                <
c:when test=&quot;${(index 1) % == 0}&quot;>

                    <
tr id=&quot;<%=&quot;fitTbl&quot;+(index.intValue()+1)%>&quotalign=&quot;center&quot; class=&quot;forumRow&quotonmouseover=&quot;selectbar(this)&quotonmouseout=&quot;unselectbar(this)&quot;  ondblclick=&quot;doEdit('<bean:write name=&quot;objs&quot; property=&quot;id&quot;/>')&quot; />

                </
c:when>

                <
c<img src="images/smilies/33.gif" smilieid="204" border="0" alt="" />therwise>

                    <
tr id=&quot;<%=&quot;fitTbl&quot;+(index.intValue()+1)%>&quotalign=&quot;center&quot; class=&quot;forumRow2&quotonmouseover=&quot;selectbar(this)&quotonmouseout=&quot;unselectbar(this)&quotondblclick=&quot;doEdit('<bean:write name=&quot;objs&quot; property=&quot;id&quot;/>')&quot;/>

                </
c<img src="images/smilies/33.gif" smilieid="204" border="0" alt="" />therwise>

            </
c:choose>




__________________
 
1、车险查勘定损系统(GPS调度、现场出单)、汽车配件报价服务

2、保险销售管理软件(规则引擎驱动、随需应变)

TEL:021-6102-6533/028-6858-2558/010-6495-3210 FAX:021-5101-2055 eMail:sutra(at)kuke.info

__________________

销售战纪 | IT十年 

同业调查 | 保险系统  

招聘开发 | 招聘销售  
只看该作者    顶部
 
    

相关内容


CopyRight 1999-2006 itpub.net All Right Reserved.
北京皓辰广域网络信息技术有限公司. 版权所有
E-mail:Webmaster@itpub.net
京ICP证:010037号 联系我们 法律顾问