技术2022-05-20  50

    扩展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)]

    OK


    最新回复(0)