重写GridView实现单击行变色的效果

继承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 >
        
& nbsp;
        
< 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 >

下面运行后的效果:

 

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值