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

简介:
+关注继续查看
GridView既强大又好用。为了让它更强大、更好用,我们来写一个继承自GridView的控件。
[索引页]
[源码下载]


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


作者:webabcd


介绍
扩展GridView控件时采用的基本架构;为GridView控件增加自定义事件;扩展GridView控件时的要点汇总


1、基本架构
定义一个抽象类,每个实现扩展功能的类都要实现这个抽象类
InBlock.gifusing System; 
InBlock.gifusing System.Collections.Generic; 
InBlock.gifusing System.Text; 
InBlock.gif 
InBlock.gifnamespace YYControls.SmartGridViewFunction 
InBlock.gif
InBlock.gif        /// <summary> 
InBlock.gif        /// 扩展功能类,抽象类 
InBlock.gif        /// </summary> 
InBlock.gif        public abstract class ExtendFunction 
InBlock.gif        { 
InBlock.gif                /// <summary> 
InBlock.gif                /// SmartGridView对象变量 
InBlock.gif                /// </summary> 
InBlock.gif                protected SmartGridView _sgv; 
InBlock.gif 
InBlock.gif                /// <summary> 
InBlock.gif                /// 构造函数 
InBlock.gif                /// </summary> 
InBlock.gif                public ExtendFunction() 
InBlock.gif                { 
InBlock.gif                         
InBlock.gif                } 
InBlock.gif 
InBlock.gif                /// <summary> 
InBlock.gif                /// 构造函数 
InBlock.gif                /// </summary> 
InBlock.gif                /// <param name="sgv">SmartGridView对象</param> 
InBlock.gif                public ExtendFunction(SmartGridView sgv) 
InBlock.gif                { 
InBlock.gif                        this._sgv = sgv; 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                /// <summary> 
InBlock.gif                /// SmartGridView对象 
InBlock.gif                /// </summary> 
InBlock.gif                public SmartGridView SmartGridView 
InBlock.gif                { 
InBlock.gif                        get { return this._sgv; } 
InBlock.gif                        set { this._sgv = value; } 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                /// <summary> 
InBlock.gif                /// 实现扩展功能 
InBlock.gif                /// </summary> 
InBlock.gif                public void Complete() 
InBlock.gif                { 
InBlock.gif                        if (this._sgv == null
InBlock.gif                        { 
InBlock.gif                                throw new ArgumentNullException("SmartGridView""扩展功能时未设置SmartGridView对象"); 
InBlock.gif                        } 
InBlock.gif                        else 
InBlock.gif                        { 
InBlock.gif                                Execute(); 
InBlock.gif                        } 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                /// <summary> 
InBlock.gif                /// 扩展功能的具体实现 
InBlock.gif                /// </summary> 
InBlock.gif                protected abstract void Execute(); 
InBlock.gif        } 
InBlock.gif}
 
如果需要为GridView扩展功能的话,只要继承这个类,并重写其Execute()方法即可

调用各个扩展功能对象的时候,可以先根据条件把需要的对象添加到List<ExtendFunction>,然后遍历它,并设置ExtendFunction的SmartGridView属性,调用ExtendFunction的Complete()方法即可


2、增加事件
RowDataBound是一个比较常用的事件,往往我们会在其内判断一下Row的RowType是否是DataRow,所以我们完全可以增加一个RowDataBoundDataRow事件(RowDataBound事件中,当Row.RowType为DataControlRowType.DataRow的时候触发)。我们还可以根据需要为GridView增加其它的事件,接下来以为GridView增加RowDataBoundDataRow事件为例说一下如何实现。
i) 添加delegate
InBlock.gifusing System; 
InBlock.gifusing System.Collections.Generic; 
InBlock.gifusing System.Text; 
InBlock.gif 
InBlock.gifusing System.Web.UI.WebControls; 
InBlock.gifusing System.Web.UI; 
InBlock.gif 
InBlock.gifnamespace YYControls 
InBlock.gif
InBlock.gif        /// <summary> 
InBlock.gif        /// SmartGridView类的委托部分 
InBlock.gif        /// </summary> 
InBlock.gif        public partial class SmartGridView 
InBlock.gif        { 
InBlock.gif                /// <summary> 
InBlock.gif                /// RowDataBoundDataRow事件委托 
InBlock.gif                /// </summary> 
InBlock.gif                /// <remarks> 
InBlock.gif                /// RowDataBound事件中的DataControlRowType.DataRow部分 
InBlock.gif                /// </remarks> 

InBlock.gif                /// <param name="sender"></param> 
InBlock.gif                /// <param name="e"></param> 
InBlock.gif                public delegate void RowDataBoundDataRowHandler(object sender, GridViewRowEventArgs e); 
InBlock.gif        } 
InBlock.gif}
 
ii) 添加event
InBlock.gifusing System; 
InBlock.gifusing System.Collections.Generic; 
InBlock.gifusing System.Text; 
InBlock.gifusing System.ComponentModel; 
InBlock.gif 
InBlock.gifusing System.Web.UI.WebControls; 
InBlock.gifusing System.Web.UI; 
InBlock.gif 
InBlock.gifnamespace YYControls 
InBlock.gif
InBlock.gif        /// <summary> 
InBlock.gif        /// SmartGridView类的事件部分 
InBlock.gif        /// </summary> 
InBlock.gif        public partial class SmartGridView 
InBlock.gif        { 
InBlock.gif                private static readonly object rowDataBoundDataRowEventKey = new object(); 
InBlock.gif                /// <summary> 
InBlock.gif                /// RowDataBound事件中的DataControlRowType.DataRow部分 
InBlock.gif                /// </summary> 
InBlock.gif                [Category("扩展")] 
InBlock.gif                public event RowDataBoundDataRowHandler RowDataBoundDataRow 
InBlock.gif                { 
InBlock.gif                        add { Events.AddHandler(rowDataBoundDataRowEventKey, value); } 
InBlock.gif                        remove { Events.RemoveHandler(rowDataBoundDataRowEventKey, value); } 
InBlock.gif                } 
InBlock.gif                /// <summary> 
InBlock.gif                /// 触发RowDataBoundDataRow事件 
InBlock.gif                /// </summary> 
InBlock.gif                /// <param name="e"></param> 
InBlock.gif                protected virtual void OnRowDataBoundDataRow(GridViewRowEventArgs e) 
InBlock.gif                { 
InBlock.gif                        RowDataBoundDataRowHandler handler = Events[rowDataBoundDataRowEventKey] as RowDataBoundDataRowHandler; 
InBlock.gif 
InBlock.gif                        if (handler != null
InBlock.gif                        { 
InBlock.gif                                handler(this, e); 
InBlock.gif                        } 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
iii) 重写GridView的OnRowDataBound
InBlock.gif /// <summary> 
InBlock.gif                /// OnRowDataBound 
InBlock.gif                /// </summary> 
InBlock.gif                /// <param name="e">e</param> 
InBlock.gif                protected override void OnRowDataBound(GridViewRowEventArgs e) 
InBlock.gif                { 
InBlock.gif                        DataControlRowType rowType = e.Row.RowType; 
InBlock.gif 
InBlock.gif                        if (rowType == DataControlRowType.DataRow) 
InBlock.gif                        { 
InBlock.gif                                OnRowDataBoundDataRow(e); 
InBlock.gif                        } 
InBlock.gif 
InBlock.gif                        base.OnRowDataBound(e); 
InBlock.gif                }
 
3、要点汇总
a) 嵌入资源
设置资源文件的“生成操作”为“嵌入的资源”
定义在程序集中启用嵌入式资源的元数据属性
[assembly: System.Web.UI.WebResource("YYControls.SmartGridView.Resources.ScriptLibrary.js", "text/javascript")]
 
使用嵌入资源
InBlock.gifif (!this.Page.ClientScript.IsClientScriptIncludeRegistered(this.GetType(), "yy_sgv_ScriptLibrary")) 
InBlock.gif
InBlock.gif        // 注册所需脚本 
InBlock.gif        this.Page.ClientScript.RegisterClientScriptInclude 
InBlock.gif        ( 
InBlock.gif                this.GetType(), 
InBlock.gif                "yy_sgv_ScriptLibrary"
InBlock.gif                this.Page.ClientScript.GetWebResourceUrl 
InBlock.gif                ( 
InBlock.gif                        this.GetType(), "YYControls.SmartGridView.Resources.ScriptLibrary.js" 
InBlock.gif                ) 
InBlock.gif        ); 
InBlock.gif
InBlock.gif// this.Page.ClientScript.RegisterClientScriptResource(this.GetType(), "YYControls.SmartGridView.ScriptLibrary.js");
 
b) Debug和Release使用不用的资源
InBlock.gif#if DEBUG 
InBlock.gif[assembly: System.Web.UI.WebResource("YYControls.SmartGridView.Resources.ScriptLibraryDebug.js""text/javascript")] 
InBlock.gif#else 
InBlock.gif[assembly: System.Web.UI.WebResource("YYControls.SmartGridView.Resources.ScriptLibrary.js""text/javascript")] 
InBlock.gif#endif
 
c) 为自定义控件添加图标
InBlock.gif[System.Drawing.ToolboxBitmap(typeof(YYControls.Resources.Icon), "SmartGridView.bmp")] 
 
d) 设置自定义控件的标记前缀
InBlock.gif[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) 复合属性
定义一个实体类,复合属性就是这个实体类对象,在复合属性上增加元数据
InBlock.gif
InBlock.gifDesignerSerializationVisibility(DesignerSerializationVisibility.Content), 
InBlock.gifPersistenceMode(PersistenceMode.InnerProperty) 
InBlock.gif]
 

g) 集合属性
定义一个继承自CollectionBase的类,集合属性就是这个类的对象,在集合属性上增加元数据
InBlock.gif
InBlock.gifDesignerSerializationVisibility(DesignerSerializationVisibility.Content), 
InBlock.gifPersistenceMode(PersistenceMode.InnerProperty) 
InBlock.gif]
 





     本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/345439,如需转载请自行联系原作者

相关文章
|
25天前
|
容灾 数据库 数据中心
单元化架构:解锁异地多活与突破扩展上限的利器
单元化架构:解锁异地多活与突破扩展上限的利器
|
1月前
|
存储 安全 块存储
ceph-架构扩展
ceph-架构扩展
30 1
|
1月前
|
负载均衡 算法 网络协议
Haproxy架构扩展
Haproxy架构扩展
25 0
|
1月前
|
网络协议 Linux 调度
lvs架构扩展
lvs架构扩展
33 0
|
1月前
|
存储 安全 NoSQL
架构扩展-ceph(1)
架构扩展-ceph(1)
63 0
|
1月前
|
监控 负载均衡 算法
架构扩展haproxy
架构扩展haproxy
47 0
|
1月前
|
网络协议 Linux 调度
架构扩展lvs
架构扩展lvs
46 0
|
1月前
|
负载均衡 监控 算法
架构扩展ha-proxy
ha-proxy是一款高性能的负载均衡软件。因为其专注于负载均衡这一些事情,因此与nginx比起来在负载均衡这件事情上做更好。
|
1月前
|
负载均衡 网络协议 Linux
架构扩展-lvs
LVS: Linux virtual server,即 Linux 虚拟服务器。 LVS 自身是个负载均衡器(director),不直接处理请求,而是将请求转发至位于它后端真正的服务器 realserver 上。 LVS 是四层(传输层 tcp/udp)负载均衡工具 ipvs 是集成在内核中的框架,可以通过用户空间的程序 ipvsadm 工具来管理,该工具可以定义一些规则来管理内核中 的 ipvs。 lvs无法解决RS的健康检查,并且存在单点故障,建议使用keepalived高可用。
|
4月前
|
机器学习/深度学习 人工智能 缓存
在Transformer时代重塑RNN,RWKV将非Transformer架构扩展到数百亿参数
在Transformer时代重塑RNN,RWKV将非Transformer架构扩展到数百亿参数
121 0
热门文章
最新文章
相关产品
云迁移中心
推荐文章
更多