重寫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>

下面運行後的效果:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章