MVC进阶学习--HtmlHelper之GridView控件拓展(三)

1.扩展核心代码
MVC进阶学习--HtmlHelper之GridView控件拓展(三)MVC进阶学习--HtmlHelper之GridView控件拓展(三)Code
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Web;
  5 using System.Web.Mvc.Html;
  6 using System.Web.Mvc;
  7 using MvcTest.Code;
  8 using System.Web.UI;
  9 using System.IO;
 10 using System.Data;
 11 
 12 namespace MvcTest.Code
 13 {
 14     public static class GridViewHelper
 15     {
 16         public static string GridView<T>(this HtmlHelper helper, PageList<T> items, string[] columns, GridViewOption option)
 17         {
 18             if (columns == null)
 19             {
 20                 columns = typeof(T).GetProperties().Select(p => p.Name).ToArray<string>();
 21             }
 22 
 23             if (option.Headers == null)
 24             {
 25                 option.Headers = columns;
 26             }
 27             else
 28             {
 29                 if (option.Headers.Length != columns.Length)
 30                 {
 31                     option.Headers = columns;
 32                 }
 33             }
 34 
 35             HtmlTextWriter writer = new HtmlTextWriter(new StringWriter());
 36             writer.RenderBeginTag(HtmlTextWriterTag.Table);
 37 
 38             //标题
 39             RenderHeander<T>(helper, writer, columns, option);
 40 
 41             //添加数据行
 42             RenderRow<T>(helper, writer, items, columns, option);
 43 
 44             //添加分页
 45             RenderPageList<T>(helper, writer, items, columns, option);
 46 
 47             return writer.InnerWriter.ToString(); ;
 48         }
 49 
 50 
 51         /// <summary>
 52         /// 添加表头行
 53         /// </summary>
 54         /// <typeparam name="T"></typeparam>
 55         /// <param name="helper"></param>
 56         /// <param name="writer"></param>
 57         /// <param name="columns"></param>
 58         /// <param name="option"></param>
 59         /// <returns></returns>
 60         public static string RenderHeander<T>(this HtmlHelper helper, HtmlTextWriter writer, string[] columns, GridViewOption option)
 61         {
 62             writer.RenderBeginTag(HtmlTextWriterTag.Tr);
 63             for (int i = 0; i < option.Headers.Length;i++ )
 64             {
 65                 writer.AddAttribute("id",columns[i]);
 66                 writer.AddAttribute("name", columns[i]);
 67                 writer.RenderBeginTag(HtmlTextWriterTag.Th);
 68                 writer.Write(option.Headers[i]);
 69                 writer.RenderEndTag();
 70             }
 71 
 72             //如果有编辑或删除列,则要在原有列的基础上加一列
 73             if (option.IsEidt || option.IsDelete)
 74             {
 75                 writer.RenderBeginTag(HtmlTextWriterTag.Th);
 76                 writer.RenderEndTag();
 77             }
 78             writer.RenderEndTag();
 79             return writer.InnerWriter.ToString();
 80         }
 81 
 82         /// <summary>
 83         /// 添加数据行
 84         /// </summary>
 85         /// <param name="helper"></param>
 86         /// <param name="writer"></param>
 87         /// <param name="item"></param>
 88         /// <param name="columns"></param>
 89         /// <param name="option"></param>
 90         /// <returns></returns>
 91         public static string RenderRow<T>(this HtmlHelper helper, HtmlTextWriter writer, PageList<T> items, string[] columns, GridViewOption option)
 92         {
 93             string value = null;
 94             foreach (var item in items)
 95             {
 96                 writer.RenderBeginTag(HtmlTextWriterTag.Tr);
 97                 for (int i = 0; i < columns.Length; i++)
 98                 {
 99                     writer.RenderBeginTag(HtmlTextWriterTag.Td);
100                     value = typeof(T).GetProperty(columns[i]).GetValue(item, null).ToString();
101                     if (i == 0)
102                     {
103                         writer.AddAttribute("type""hidden");
104                         writer.AddAttribute("id",items.IdentityName);
105                         writer.AddAttribute("name", items.IdentityName);
106                         writer.AddAttribute("value"typeof(T).GetProperty(items.IdentityName).GetValue(item, null).ToString());
107                         writer.RenderBeginTag(HtmlTextWriterTag.Input);
108                         writer.RenderEndTag();
109                         writer.Write(value.Length > 20 ? value.Substring(020) : value);
110                     }
111                     else
112                     {
113                         writer.Write(value.Length > 20 ? value.Substring(020) : value);
114                     }
115                     //writer.Write(value);
116                     writer.RenderEndTag();
117                 }
118                 if (option.IsEidt || option.IsDelete)
119                 {
120                     writer.RenderBeginTag(HtmlTextWriterTag.Td);
121                     if (option.IsEidt)
122                     {
123                         //writer.Write(helper.ActionLink(option.EditButton, null, null, new { @class = "Edit_Link" }) + "   ");
124                         writer.Write("<a href='#' class='Edit_Link'>编辑</a>&nbsp;&nbsp;&nbsp;");
125                     }
126                     if (option.IsDelete)
127                     {
128                         writer.Write(helper.ActionLink(option.DeleteButton, option.DeleteAction, new { controller = option.DeleteController, id = typeof(T).GetProperty(items.IdentityName).GetValue(item, null).ToString() }));
129                     }
130 
131                     writer.RenderEndTag();
132                 }
133                 writer.RenderEndTag();
134             }
135             return writer.InnerWriter.ToString();
136         }
137 
138 
139         /// <summary>
140         /// 添加分页
141         /// </summary>
142         /// <param name="helper"></param>
143         /// <param name="writer"></param>
144         /// <param name="option"></param>
145         /// <returns></returns>
146         public static string RenderPageList<T>(this HtmlHelper helper, HtmlTextWriter writer, PageList<T> items, string[] columns, GridViewOption option)
147         {
148             writer.RenderBeginTag(HtmlTextWriterTag.Tr);
149             if (option.IsEidt || option.IsDelete)
150             {
151                 writer.AddAttribute("colspan", (option.Headers.Length + 1).ToString());
152             }
153             else
154             {
155                 writer.AddAttribute("colspan", (option.Headers.Length).ToString());
156             }
157             writer.AddAttribute("align""center");
158             writer.RenderBeginTag(HtmlTextWriterTag.Td);
159             writer.Write(helper.ActionLink("首页", items.Action, new { controller = items.Controller, pageIndex = 1 }) + "&nbsp;&nbsp;&nbsp;&nbsp;");
160             writer.Write(helper.ActionLink("上一页", items.Action, new { controller = items.Controller, pageIndex = items.Page.PageIndex - 1 }) + "&nbsp;&nbsp;&nbsp;&nbsp;");
161             writer.Write(items.Page.PageIndex + "/" + items.Page.PageCount + "页&nbsp;&nbsp;&nbsp;&nbsp;");
162             writer.Write(helper.ActionLink("下一页", items.Action, new { controller = items.Controller, pageIndex = items.Page.PageIndex + 1 }) + "&nbsp;&nbsp;&nbsp;&nbsp;");
163             writer.Write(helper.ActionLink("尾页", items.Action, new { controller = items.Controller, pageIndex = items.Page.PageCount }) + "&nbsp;&nbsp;&nbsp;&nbsp;");
164             writer.RenderEndTag();
165             writer.RenderEndTag();
166             writer.RenderEndTag();
167 
168             //构建隐藏表单
169             writer.AddStyleAttribute("display","none");
170             writer.RenderBeginTag(HtmlTextWriterTag.Div);
171             writer.AddAttribute("id","form0");
172             writer.AddAttribute("action","/Home/Update");
173             writer.AddAttribute("method","post");
174             writer.RenderBeginTag(HtmlTextWriterTag.Form);
175             foreach (string column in columns)
176             {
177                 writer.AddAttribute("type","hidden");
178                 writer.AddAttribute("id", column);
179                 writer.AddAttribute("name", column);
180                 writer.AddAttribute("value""");
181                 writer.RenderBeginTag(HtmlTextWriterTag.Input);
182                 writer.RenderEndTag();
183             }
184             writer.RenderEndTag();
185             writer.RenderEndTag();
186 
187             return writer.InnerWriter.ToString();
188         }
189     }
190 }
191 
(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(); ;

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


更多方法介绍看下篇

上一篇:MVC进阶学习--HtmlHelper控件解析(四)


下一篇:网站与APP抓包分析2 分析方法案例