GridView既強大又好用。為了讓它更強大、更好用,我們來寫一個繼承自GridView的控件。
<a href="http://webabcd.blog.51cto.com/1787395/345429" target="_blank">[索引頁]</a>
<a href="http://down.51cto.com/data/101204" target="_blank">[源碼下載下傳]</a>
擴充GridView控件(0) - 基本架構、增加事件和要點彙總
介紹
擴充GridView控件時采用的基本架構;為GridView控件增加自定義事件;擴充GridView控件時的要點彙總
1、基本架構
定義一個抽象類,每個實作擴充功能的類都要實作這個抽象類
using System;
using System.Collections.Generic;
using System.Text;
namespace YYControls.SmartGridViewFunction
{
/// <summary>
/// 擴充功能類,抽象類
/// </summary>
public abstract class ExtendFunction
{
/// <summary>
/// SmartGridView對象變量
/// </summary>
protected SmartGridView _sgv;
/// 構造函數
public ExtendFunction()
{
}
/// <param name="sgv">SmartGridView對象</param>
public ExtendFunction(SmartGridView sgv)
this._sgv = sgv;
/// SmartGridView對象
public SmartGridView SmartGridView
get { return this._sgv; }
set { this._sgv = value; }
/// 實作擴充功能
public void Complete()
if (this._sgv == null)
{
throw new ArgumentNullException("SmartGridView", "擴充功能時未設定SmartGridView對象");
}
else
Execute();
/// 擴充功能的具體實作
protected abstract void Execute();
}
}
如果需要為GridView擴充功能的話,隻要繼承這個類,并重寫其Execute()方法即可
調用各個擴充功能對象的時候,可以先根據條件把需要的對象添加到List<ExtendFunction>,然後周遊它,并設定ExtendFunction的SmartGridView屬性,調用ExtendFunction的Complete()方法即可
2、增加事件
RowDataBound是一個比較常用的事件,往往我們會在其内判斷一下Row的RowType是否是DataRow,是以我們完全可以增加一個RowDataBoundDataRow事件(RowDataBound事件中,當Row.RowType為DataControlRowType.DataRow的時候觸發)。我們還可以根據需要為GridView增加其它的事件,接下來以為GridView增加RowDataBoundDataRow事件為例說一下如何實作。
i) 添加delegate
using System.Web.UI.WebControls;
using System.Web.UI;
namespace YYControls
/// SmartGridView類的委托部分
public partial class SmartGridView
/// RowDataBoundDataRow事件委托
/// <remarks>
/// RowDataBound事件中的DataControlRowType.DataRow部分
/// </remarks>
/// <param name="sender"></param>
/// <param name="e"></param>
public delegate void RowDataBoundDataRowHandler(object sender, GridViewRowEventArgs e);
ii) 添加event
using System.ComponentModel;
/// SmartGridView類的事件部分
private static readonly object rowDataBoundDataRowEventKey = new object();
/// RowDataBound事件中的DataControlRowType.DataRow部分
[Category("擴充")]
public event RowDataBoundDataRowHandler RowDataBoundDataRow
add { Events.AddHandler(rowDataBoundDataRowEventKey, value); }
remove { Events.RemoveHandler(rowDataBoundDataRowEventKey, value); }
/// 觸發RowDataBoundDataRow事件
protected virtual void OnRowDataBoundDataRow(GridViewRowEventArgs e)
RowDataBoundDataRowHandler handler = Events[rowDataBoundDataRowEventKey] as RowDataBoundDataRowHandler;
if (handler != null)
handler(this, e);
iii) 重寫GridView的OnRowDataBound
/// <summary>
/// OnRowDataBound
/// <param name="e">e</param>
protected override void OnRowDataBound(GridViewRowEventArgs e)
DataControlRowType rowType = e.Row.RowType;
if (rowType == DataControlRowType.DataRow)
OnRowDataBoundDataRow(e);
base.OnRowDataBound(e);
}
3、要點彙總
a) 嵌入資源
設定資源檔案的“生成操作”為“嵌入的資源”
定義在程式集中啟用嵌入式資源的中繼資料屬性
[assembly: System.Web.UI.WebResource("YYControls.SmartGridView.Resources.ScriptLibrary.js", "text/javascript")]
使用嵌入資源
if (!this.Page.ClientScript.IsClientScriptIncludeRegistered(this.GetType(), "yy_sgv_ScriptLibrary"))
// 注冊所需腳本
this.Page.ClientScript.RegisterClientScriptInclude
(
this.GetType(),
"yy_sgv_ScriptLibrary",
this.Page.ClientScript.GetWebResourceUrl
(
this.GetType(), "YYControls.SmartGridView.Resources.ScriptLibrary.js"
)
);
}
// this.Page.ClientScript.RegisterClientScriptResource(this.GetType(), "YYControls.SmartGridView.ScriptLibrary.js");
b) Debug和Release使用不用的資源
#if DEBUG
[assembly: System.Web.UI.WebResource("YYControls.SmartGridView.Resources.ScriptLibraryDebug.js", "text/javascript")]
#else
[assembly: System.Web.UI.WebResource("YYControls.SmartGridView.Resources.ScriptLibrary.js", "text/javascript")]
#endif
c) 為自定義控件添加圖示
[System.Drawing.ToolboxBitmap(typeof(YYControls.Resources.Icon), "SmartGridView.bmp")]
d) 設定自定義控件的标記字首
[assembly: TagPrefix("YYControls", "yyc")]
e) 常用中繼資料
Browsable - 指定一個屬性 (Property) 或事件是否應顯示在“屬性”視窗中
Description - 指定屬性 (Property) 或事件的說明
Category - 給屬性或事件分組的類别的名稱
NotifyParentProperty - 訓示當此屬性應用到的屬性的值被修改時将通知父屬性
DefaultValue - 指定屬性 (Property) 的預設值
Editor - 指定用來更改屬性的編輯器
ToolboxItem - 表示工具箱項的屬性
ToolboxData - 指定當從 Microsoft Visual Studio 等工具中的工具箱拖動自定義控件時為它生成的預設标記
TypeConverter - 指定用作此屬性所綁定到的對象的轉換器的類型(一般是[TypeConverter(typeof(ExpandableObjectConverter))])
DesignerSerializationVisibility - 指定在設計時序列化元件上的屬性 (Property) 時所使用的持久性類型
PersistenceMode - 定義指定如何将 ASP.NET 伺服器控件屬性 (Property) 或事件保持到 ASP.NET 頁的中繼資料特性 (Attribute)
ParseChildren - 訓示頁分析器應如何處理頁上聲明的伺服器控件标記中嵌套的内容
PersistChildren - 訓示在設計時伺服器控件中包含的嵌套内容是與控件對應,還是作為伺服器控件的屬性 (Property)
f) 複合屬性
定義一個實體類,複合屬性就是這個實體類對象,在複合屬性上增加中繼資料
[
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
PersistenceMode(PersistenceMode.InnerProperty)
]
g) 集合屬性
定義一個繼承自CollectionBase的類,集合屬性就是這個類的對象,在集合屬性上增加中繼資料
OK
本文轉自webabcd 51CTO部落格,原文連結:http://blog.51cto.com/webabcd/345439,如需轉載請自行聯系原作者