继承GridView控件加入ClickBackGroundColor和ClickFontColor属性,其中ClickBackGroundColor属性是设置单击当前行时的背景色,ClickFontColor是设置单击当前行时的字体颜色.默认单击行不变色,代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
namespace MyLabel
...{
public class MyGridView:GridView
...{
public MyGridView()
...{
}
[Browsable(true)]
[Category("Appearance")]
[Description("设置单击当前行时当前行的背景色")]
public virtual Color ClickBackGroundColor
...{
get
...{
return ViewState["ClickBackGroundColor"]!=null?(Color)ViewState["ClickBackGroundColor"]:Color.Empty;
}
set
...{
ViewState["ClickBackGroundColor"] = value;
}
}
[Browsable(true)]
[Category("Appearance")]
[Description("设置单击当前行时当前行的字体背景色")]
public virtual Color ClickFontColor
...{
get
...{
return ViewState["ClickFontColor"] != null ? (Color)ViewState["ClickFontColor"] : Color.Empty;
}
set
...{
ViewState["ClickFontColor"] = value;
}
}
protected override void OnRowDataBound(GridViewRowEventArgs e)
...{
if (ClickBackGroundColor != null && ClickFontColor != null)
...{
if (e.Row.RowType == DataControlRowType.DataRow)
...{
if (e.Row.RowIndex != -1)
...{
e.Row.Attributes["onclick"] = "if(window.oldtr!=null){window.oldtr.runtimeStyle.cssText= ' ';}this.runtimeStyle.cssText= 'background-color:" + ClickBackGroundColor.ToString() + ";color:" + ClickFontColor.ToString() + " ';window.oldtr=this ";
}
}
}
base.OnRowDataBound(e);
}
}
}

前台代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Demo.aspx.cs" Inherits="Demo" %>
<%@ Register Assembly="MyLabel" Namespace="MyLabel" TagPrefix="cc1" %>
<%@ Register Src="AspNetPager.ascx" TagName="AspNetPager" TagPrefix="uc1" %>


<!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 runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<cc1:MyGridView ID="GridView1" runat="server" AllowPaging="false" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" PagerSettings-Visible="false"
Width="100%" height="35" ClickBackGroundColor="#ffccff" ClickFontColor="#ffffff">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle ForeColor="White" VerticalAlign="Top" BackColor="Transparent" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="序号">
<ItemTemplate>
<%# Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="I_BriefnessID" HeaderText="ID" />
<asp:BoundField DataField="I_KMID" HeaderText="科目" />
<asp:BoundField DataField="C_Recno" HeaderText="试题号" />
<asp:BoundField DataField="M_Title" HeaderText="题面" />
<asp:BoundField DataField="C_Answer" HeaderText="答案" /> 
</Columns>
</cc1:MyGridView>
</div>
<div>
<uc1:AspNetPager ID="AspNetPager1" runat="server" />
</div>
</form>
</body>
</html>
下面运行后的效果:

本文介绍了一种通过继承ASP.NET中的GridView控件来自定义点击行时背景色和字体颜色的方法,并提供了具体的实现代码及示例。
2663

被折叠的 条评论
为什么被折叠?



