在head头里加js
<script type="text/javascript"> //把事件放在onload里, //使用<%=%>方式输出GridView的ID是因为某些情况下(如使用了MasterPage)会造成HTML中ID的变化 //颜色值推荐使用Hex,如 #f00 或 #ff0000 window.onload = function(){ GridViewColor("<%=GridView1.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>
或者GridView1的RowDataBound事件添加
//如果是绑定数据行,添加确认对话框 //<asp:Button ID="btnRefuse" runat="server" OnClick="btnRefuse_Click" Text="拒绝" OnClientClick="return confirm(' 你真的要删除?')"/> if (e.Row.RowType == DataControlRowType.DataRow) {
//离开显示原背景色,可用
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#6699FF'");//当鼠标停留时更改背景色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");//当鼠标移开时还原背景色
//鼠标移动到每项时颜色交替效果 e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor='White';this.style.color='#003399'"); e.Row.Attributes.Add("OnMouseOver", "this.style.backgroundColor='#6699FF';this.style.color='#8C4510'"); // e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='Azure',
this.style.fontWeight='';");
//e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';"); //不能同时测试:单击/双击 事件, 要分别测试它们 //e.Row.Attributes.Add("OnDblClick", "DbClickEvent('" + e.Row.Cells[1].Text + "')"); e.Row.Attributes.Add("OnClick", "ClickEvent('" + e.Row.Cells[1].Text + "')"); //e.Row.Attributes.Add("OnKeyDown", "GridViewItemKeyDownEvent('" + e.Row.Cells[1].Text + "')"); e.Row.Attributes["style"] = "Cursor:hand"; /// if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate) // { // ((LinkButton)e.Row.Cells[8].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除:\"" + e.Row.Cells[1].Text + "\"吗?')"); // } } //遍历所有行设置边框样式 foreach (TableCell tc in e.Row.Cells) { tc.Attributes["style"] = "border-color:Black"; //单击鼠标可以将单元格中的内容复制到剪切板 tc.Attributes.Add("onclick", "window.clipboardData.setData('Text', this.innerText);"); //鼠标移入可以设置单元格字体颜色 tc.Attributes.Add("onmouseover", "this.style.color='red'"); //鼠标移开,取消单元格字体颜色设置 tc.Attributes.Add("onmouseout", "this.style.color=''"); } //用索引来取得编号 if (e.Row.RowIndex != -1) { int id = GridView1.PageIndex * GridView1.PageSize + e.Row.RowIndex + 1; e.Row.Cells[0].Text = id.ToString(); }