js 改变gridview样式
2009-09-14 10:22
369 查看
<%@ 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[i].style.background = i%2==0?NormalColor:AlterColor;
//如果指定了鼠标指向的背景色,则添加onmouseover/onmouseout事件
//处于选中状态的行发生这两个事件时不改变颜色
if(HoverColor != "")
{
AllRows[i].onmouseover = function(){if(!this.selected)this.style.background = HoverColor;}
if(i%2 == 0)
{
AllRows[i].onmouseout = function(){if(!this.selected)this.style.background = NormalColor;}
}
else
{
AllRows[i].onmouseout = function(){if(!this.selected)this.style.background = AlterColor;}
}
}
//如果指定了鼠标点击的背景色,则添加onclick事件
//在事件响应中修改被点击行的选中状态
if(SelectColor != "")
{
AllRows[i].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();
}
}
<!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[i].style.background = i%2==0?NormalColor:AlterColor;
//如果指定了鼠标指向的背景色,则添加onmouseover/onmouseout事件
//处于选中状态的行发生这两个事件时不改变颜色
if(HoverColor != "")
{
AllRows[i].onmouseover = function(){if(!this.selected)this.style.background = HoverColor;}
if(i%2 == 0)
{
AllRows[i].onmouseout = function(){if(!this.selected)this.style.background = NormalColor;}
}
else
{
AllRows[i].onmouseout = function(){if(!this.selected)this.style.background = AlterColor;}
}
}
//如果指定了鼠标点击的背景色,则添加onclick事件
//在事件响应中修改被点击行的选中状态
if(SelectColor != "")
{
AllRows[i].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();
}
}
相关文章推荐
- 扩展GridView控件(1) - 鼠标经过行时改变行的样式
- 使用jquery.fullpage.js需要动态改变导航小圆点的样式
- 使用js改变DOM样式的三种方式 - 动态创建style标签
- GridView根据值的变化改变行列样式
- DevExpress 改变GridView选中的表格的边框样式
- GridView根据值的变化改变行列样式
- GridView根据值的变化改变行列样式
- js改变/获取html标签样式
- JS 改变鼠标样式
- vue2.0实现引用qrcode.js实现获取改变二维码的样式(2)
- js改变style样式和css样式的简单实例
- js改变DOM样式的三种方式
- GridView根据值的变化改变行列样式
- js如何通过tagname取到标签并改变所有该类标签的样式
- JS获取样式 改变样式
- jQuery改变GridView的样式
- JS实现动态改变对话窗口和图片样式的聊天;
- 当鼠标移到DataGrid(2003.net)、GridView(2005.net)行时,改变样式
- [微信小程序]js动态改变数组对象列表中的样式
- GridView里的一点小功能:截取多余字符、改变鼠标经过行的样式