zoukankan      html  css  js  c++  java
  • MVC进阶学习HtmlHelper之GridView控件拓展(三)

    1.扩展核心代码
    Code
    (1)
    public static string GridView<T>(this HtmlHelper helper, PageList<T> items, string[] columns, GridViewOption option)
    this HtmlHelper helper  这个是.net3.0 中的新特性,我们扩展HtmlHelper这个类,使之具有GridView<T>()这个泛型方法
    PageList<T> items   则是指定的数据源(上篇提到过的自定义的数据集合),
    string[] columns      这个是指定GridView 显示哪几列,和GridViewOption中的header数组有区别,这个是针对数据库字段或者某个实体的属性,而GridViewOption中的是表格的头部不封显示的字样
    (2)

    if (columns == null)
                {
                    columns = typeof(T).GetProperties().Select(p => p.Name).ToArray<string>();
                }

                if (option.Headers == null)
                {
                    option.Headers = columns;
                }
                else
                {
                    if (option.Headers.Length != columns.Length)
                    {
                        option.Headers = columns;
                    }
                }

    这个定义了表格标题的匹配规则,如果columns 的值为null,则默认显示该对象的所有属性,这里区分columns和heander数组,同时也匹配了二者之间的关系。如果这两个数组的长度相等,则表头显示自定义的字样,否则都以columns中的字样为主
    (3)

                 HtmlTextWriter writer = new HtmlTextWriter(new StringWriter());
                writer.RenderBeginTag(HtmlTextWriterTag.Table);

                //标题
                RenderHeander<T>(helper, writer, columns, option);

                //添加数据行
                RenderRow<T>(helper, writer, items, columns, option);

                //添加分页
                RenderPageList<T>(helper, writer, items, columns, option);

                return writer.InnerWriter.ToString(); ;

               这段就是调用下面的方法,设置数据


    更多方法介绍看下篇

  • 相关阅读:
    transition
    Java自增陷阱
    不同编码格式中,字节和字符的关系
    表单重复提交
    source folder和package的区别
    @test 测试案例不能添加参数
    http协议content-type
    jdbc的缺点和mybatis的优点
    ==和equals的区别
    spring IOC和AOP
  • 原文地址:https://www.cnblogs.com/qingyuan/p/1584049.html
Copyright © 2011-2022 走看看