展GridVIEw控件(0) - 基本架構、增加事件和要點匯總
作者:webabcd
介紹
擴展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;



/**//// <summary>

/// 構造函數

/// </summary>

public ExtendFunction()


{

}



/**//// <summary>

/// 構造函數

/// </summary>

/// <param name="sgv">SmartGridVIEw對象</param>

public ExtendFunction(SmartGridVIEw sgv)


{

this._sgv = sgv;

}



/**//// <summary>

/// SmartGridVIEw對象

; /// </summary>

public SmartGridView SmartGridVIEw


{


get

{ return this._sgv; }


set

{ this._sgv = value; }

}



/**//// <summary>

/// 實現擴展功能

/// </summary>

public void Complete()


{

if (this._sgv == null)

{

throw new ArgumentNullException("SmartGridView", "擴展功能時未設置SmartGridVIEw對象");

}

else


{

Execute();

}

}



/**//// <summary>

/// 擴展功能的具體實現

/// </summary>

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;

using System.Collections.Generic;

using System.Text;


using System.Web.UI.WebControls;

using System.Web.UI;


namespace YYControls



{


/**//// <summary>

/// SmartGridVIEw類的委托部分

/// </summary>

public partial class SmartGridVIEw


{


/**//// <summary>

/// RowDataBoundDataRow事件委托

/// </summary>

/// <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;

using System.Collections.Generic;

using System.Text;

using System.ComponentModel;


using System.Web.UI.WebControls;

using System.Web.UI;


namespace YYControls



{


/**//// <summary>

/// SmartGridVIEw類的事件部分

/// </summary>

public partial class SmartGridVIEw


{

private static readonly object rowDataBoundDataRowEventKey = new object();


/**//// <summary>

/// RowDataBound事件中的DataControlRowType.DataRow部分

/// </summary>

[Category("擴展")]

public event RowDataBoundDataRowHandler RowDataBoundDataRow

{


add

{ Events.AddHandler(rowDataBoundDataRowEventKey, value); }


remove

{ Events.RemoveHandler(rowDataBoundDataRowEventKey, value); }

}


/**//// <summary>

/// 觸發RowDataBoundDataRow事件

/// </summary>

/// <param name="e"></param>

protected virtual void OnRowDataBoundDataRow(GridVIEwRowEventArgs e)


{

RowDataBoundDataRowHandler handler = Events[rowDataBoundDataRowEventKey] as RowDataBoundDataRowHandler;


if (handler != null)

{

handler(this, e);

}

}

}

}

iii) 重寫GridVIEw的OnRowDataBound


/**//// <summary>

/// OnRowDataBound

/// </summary>

/// <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的類,集合屬性就是這個類的對象,在集合屬性上增加元數據

[

DesignerSerializationVisibility(DesignerSerializationVisibility.Content),

PersistenceMode(PersistenceMode.InnerProperty)

]