zoukankan      html  css  js  c++  java
  • 扩展GridView控件(0) 基本架构、增加事件和要点汇总

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


    扩展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
    [源码下载] 
  • 相关阅读:
    hdu 2680(最短路)
    hdu 1548(最短路)
    hdu 1596(最短路变形)
    hdu 1546(dijkstra)
    hdu 3790(SPFA)
    hdu 2544(SPFA)
    CodeForces 597B Restaurant
    CodeForces 597A Divisibility
    CodeForces 598E Chocolate Bar
    CodeForces 598D Igor In the Museum
  • 原文地址:https://www.cnblogs.com/webabcd/p/847235.html
Copyright © 2011-2022 走看看