天天看点

扩展GridView控件(0) - 基本架构、增加事件和要点汇总

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 

        /// &lt;summary&gt; 

        /// 扩展功能类,抽象类 

        /// &lt;/summary&gt; 

        public abstract class ExtendFunction 

        { 

                /// &lt;summary&gt; 

                /// SmartGridView对象变量 

                /// &lt;/summary&gt; 

                protected SmartGridView _sgv; 

                /// 构造函数 

                public ExtendFunction() 

                { 

                } 

                /// &lt;param name="sgv"&gt;SmartGridView对象&lt;/param&gt; 

                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&lt;ExtendFunction&gt;,然后遍历它,并设置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事件委托 

                /// &lt;remarks&gt; 

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

                /// &lt;/remarks&gt; 

                /// &lt;param name="sender"&gt;&lt;/param&gt; 

                /// &lt;param name="e"&gt;&lt;/param&gt; 

                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

 /// &lt;summary&gt; 

                /// OnRowDataBound 

                /// &lt;param name="e"&gt;e&lt;/param&gt; 

                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,如需转载请自行联系原作者

继续阅读