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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
控件使用<br>1、鼠标经过行的时候改变该行的样式,鼠标离开行的时候恢复该行的样式<br>使用方法(设置属性): <br>MouseOverCssClass - 鼠标经过行时行的 CSS 类名<br><br>2、对多个字段进行复合排序;升序、降序的排序状态提示<br>使用方法(设置SmartSorting复合属性): <br>AllowSortTip - 是否启用排序提示 <br>AllowMultiSorting - 是否启用复合排序 <br>SortAscImageUrl - 升序提示图片的URL(不设置则使用默认图片) <br>SortDescImageUrl - 降序提示图片的URL(不设置则使用默认图片) <br>SortAscText - 升序提示文本 <br>SortDescText - 降序提示文本<br><br>3、根据按钮的CommandName设置其客户端属性<br>使用方法(设置ClientButtons集合属性): <br>BoundCommandName - 需要绑定的CommandName <br>AttributeKey - 属性的名称 <br>AttributeValue - 属性的值(两个占位符:{0} - CommandArgument;{1} - Text) <br>Position - 属性的值的位置<br><br>4、联动复选框(复选框的全选和取消全选)。选中指定的父复选框,则设置指定的所有子复选框为选中状态;取消选中指定的父复选框,则设置指定的所有子复选框为取消选中状态。如果指定的所有子复选框为均选中状态,则设置指定的父复选框为选中状态;如果指定的所有子复选框至少有一个为取消选中状态,则设置指定的父复选框为取消选中状态<br>使用方法(设置CascadeCheckboxes集合属性): <br>ParentCheckboxID - 模板列中 父复选框ID <br>ChildCheckboxID - 模板列中 子复选框ID <br>YYControls.Helper.SmartGridView中的静态方法 <br>List GetCheckedDataKey(GridView gv, int columnIndex) <br>List GetCheckedDataKey(GridView gv, string checkboxId)<br><br>5、固定指定行、指定列,根据RowType固定行,根据RowState固定行 <br>使用方法(设置FixRowColumn复合属性): <br>FixRowType - 需要固定的行的RowType(用逗号“,”分隔) <br>FixRowState - 需要固定的行的RowState(用逗号“,”分隔) <br>FixRows - 需要固定的行的索引(用逗号“,”分隔) <br>FixColumns - 需要固定的列的索引(用逗号“,”分隔) <br>TableWidth - 表格的宽度 <br>TableHeight - 表格的高度<br><br>6、响应行的单击事件和双击事件,并在服务端处理 <br>使用方法(设置属性): <br>BoundRowClickCommandName - 行的单击事件需要绑定的CommandName <br>BoundRowDoubleClickCommandName - 行的双击事件需要绑定的CommandName<br><br>7、行的指定复选框选中的时候改变该行的样式,行的指定复选框取消选中的时候恢复该行的样式 <br>使用方法(设置CheckedRowCssClass复合属性): <br>CheckBoxID - 模板列中 数据行的复选框ID <br>CssClass - 选中的行的 CSS 类名<br><br>8、导出数据源的数据为Excel、Word或Text(应保证数据源的类型为DataTable或DataSet) <br>使用方法: <br>为SmartGridView添加的方法<br>Export(string fileName)<br>Export(string fileName, ExportFormat exportFormat)<br>Export(string fileName, ExportFormat exportFormat, Encoding encoding)<br>Export(string fileName, int[] columnIndexList, ExportFormat exportFormat, Encoding encoding)<br>Export(string fileName, int[] columnIndexList, string[] headers, ExportFormat exportFormat, Encoding encoding)<br><br>9、给数据行增加右键菜单,响应服务端事件或超级链接 <br>使用方法(设置ContextMenus集合属性): <br>Text - 菜单的文本内容 <br>BoundCommandName - 需要绑定的CommandName <br>NavigateUrl - 链接的URL <br>Target - 链接的目标窗口或框架 <br>SmartGridView的属性ContextMenuCssClass - 右键菜单的级联样式表 CSS 类名(右键菜单的结构div ul li a)<br><br>10、自定义分页样式。显示总记录数、每页记录数、当前页数、总页数、首页、上一页、下一页、末页和分页按钮 <br>使用方法(设置CustomPagerSettings复合属性): <br>PagingMode - 自定义分页的显示模式 <br>TextFormat - 自定义分页的文本显示样式(四个占位符:{0}-每页显示记录数;{1}-总记录数;{2}-当前页数;{3}-总页数)<br><br>11、合并指定列的相邻且内容相同的单元格<br>使用方法(设置属性): <br>MergeCells - 需要合并单元格的列的索引(用逗号“,”分隔)<br>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值