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

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


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


作者: webabcd


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


1、基本架构
定义一个抽象类,每个实现扩展功能的类都要实现这个抽象类
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Text; 
InBlock.gif 
InBlock.gif namespace 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.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Text; 
InBlock.gif 
InBlock.gif using System.Web.UI.WebControls; 
InBlock.gif using System.Web.UI; 
InBlock.gif 
InBlock.gif namespace 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.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Text; 
InBlock.gif using System.ComponentModel; 
InBlock.gif 
InBlock.gif using System.Web.UI.WebControls; 
InBlock.gif using System.Web.UI; 
InBlock.gif 
InBlock.gif namespace 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.gif if (! 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 ,如需转载请自行联系原作者

相关文章
|
1月前
|
负载均衡 测试技术 持续交付
高效后端开发实践:构建可扩展的微服务架构
在当今快速发展的互联网时代,后端开发扮演着至关重要的角色。本文将重点探讨如何构建可扩展的微服务架构,以及在后端开发中提高效率的一些实践方法。通过合理的架构设计和技术选型,我们可以更好地应对日益复杂的业务需求,实现高效可靠的后端系统。
|
1月前
|
监控 持续交付 API
构建高效可扩展的微服务架构
在当今快速迭代和竞争激烈的软件市场中,构建一个高效、可扩展且易于维护的后端系统变得尤为重要。微服务架构作为一种流行的分布式系统设计方式,允许开发者将应用程序划分为一系列小型、自治的服务,每个服务负责执行特定的业务功能。本文将探讨如何利用现代技术栈搭建一个符合这些要求的微服务架构,并讨论其潜在的挑战与解决方案。我们将涵盖服务划分策略、容器化、服务发现、API网关、持续集成/持续部署(CI/CD)以及监控和日志管理等关键主题,以帮助读者构建出既可靠又灵活的后端系统。
|
1月前
|
监控 Kubernetes 持续交付
构建高效可扩展的微服务架构:后端开发实践指南
在数字化转型的浪潮中,企业对软件系统的要求日益提高,追求快速响应市场变化、持续交付价值成为核心竞争力。微服务架构以其灵活性、模块化和独立部署的特点,成为解决复杂系统问题的有效途径。本文将深入探讨如何构建一个高效且可扩展的微服务架构,涵盖关键设计原则、技术选型及实践案例,为后端开发者提供一条清晰的指导路线,帮助其在不断变化的技术环境中保持竞争力。
131 3
|
2月前
|
运维 监控 数据管理
Apollo与微服务架构:构建可扩展的应用程序
Apollo与微服务架构:构建可扩展的应用程序
|
2月前
|
缓存 分布式计算 负载均衡
构建高效可扩展的后端系统架构
【2月更文挑战第9天】本文将介绍如何构建一种高效可扩展的后端系统架构,以满足不断增长的用户需求和应对大规模并发请求。我们将讨论关键的技术要点,包括分布式计算、负载均衡、缓存和数据库优化等,帮助读者在设计和开发后端系统时做出明智的决策。
|
24天前
|
存储 缓存 监控
构建高效可扩展的后端服务架构
在当今互联网时代,构建高效可扩展的后端服务架构对于企业的业务发展至关重要。本文将探讨如何通过合理设计和优化后端服务架构,实现系统的高性能、高可用性和易扩展性,从而满足不断增长的业务需求和用户规模。
18 0
|
12天前
|
运维 监控 自动驾驶
构建可扩展的应用程序:Apollo与微服务架构的完美结合
构建可扩展的应用程序:Apollo与微服务架构的完美结合
32 10
|
23天前
|
负载均衡 网络协议 Java
构建高效可扩展的微服务架构:利用Spring Cloud实现服务发现与负载均衡
本文将探讨如何利用Spring Cloud技术实现微服务架构中的服务发现与负载均衡,通过注册中心来管理服务的注册与发现,并通过负载均衡策略实现请求的分发,从而构建高效可扩展的微服务系统。
|
1月前
|
监控 Kubernetes Docker
构建高效可扩展的微服务架构
本文将详细介绍如何构建高效可扩展的微服务架构,包括设计原则、技术选型和实施步骤。通过采用微服务架构,企业可以实现系统的解耦、灵活性和可伸缩性,提升开发效率和系统性能。
|
2月前
|
算法 关系型数据库 MySQL
三高Mysql - 搭建“三高”架构之扩展与切换
三高Mysql - 搭建“三高”架构之扩展与切换
68 0