1.输出表头
Code
1 /// <summary>
2 /// 添加表头行
3 /// </summary>
4 /// <typeparam name="T"></typeparam>
5 /// <param name="helper"></param>
6 /// <param name="writer"></param>
7 /// <param name="columns"></param>
8 /// <param name="option"></param>
9 /// <returns></returns>
10 public static string RenderHeander<T>(this HtmlHelper helper, HtmlTextWriter writer, string[] columns, GridViewOption option)
11 {
12 writer.RenderBeginTag(HtmlTextWriterTag.Tr);
13 for (int i = 0; i < option.Headers.Length;i++ )
14 {
15 writer.AddAttribute("id",columns[i]);
16 writer.AddAttribute("name", columns[i]);
17 writer.RenderBeginTag(HtmlTextWriterTag.Th);
18 writer.Write(option.Headers[i]);
19 writer.RenderEndTag();
20 }
21
22 //如果有编辑或删除列,则要在原有列的基础上加一列
23 if (option.IsEidt || option.IsDelete)
24 {
25 writer.RenderBeginTag(HtmlTextWriterTag.Th);
26 writer.RenderEndTag();
27 }
28 writer.RenderEndTag();
29 return writer.InnerWriter.ToString();
30 }
31
这个方法主要适用于添加表头的。这里我们常用的一个类,用于从后台输出标签的类HtmlTextWriter
HtmlTextWriterlei 类中有两个方法 RenderBeginTag() 和RenderEndTag()两个方法,这两个方法分别是用于输出开始标签和结束标签的,如writer.RenderBeginTag(HtmlTextWriterTag.Tr); 输出的为Tr标签<tr>,writer.RenderEndTag();则用于输出</tr>. 如果用于输出属性
writer.AddAttribute("type", "hidden");
writer.AddAttribute("id",items.IdentityName);
writer.AddAttribute("name", items.IdentityName);
注意这里的几句话必须在writer.RenderBeginTag() 上面,否则不能输出属性
2.添加数据行
1 /// <summary>
2 /// 添加数据行
3 /// </summary>
4 /// <param name="helper"></param>
5 /// <param name="writer"></param>
6 /// <param name="item"></param>
7 /// <param name="columns"></param>
8 /// <param name="option"></param>
9 /// <returns></returns>
10 public static string RenderRow<T>(this HtmlHelper helper, HtmlTextWriter writer, PageList<T> items, string[] columns, GridViewOption option)
11 {
12 string value = null;
13 foreach (var item in items)
14 {
15 writer.RenderBeginTag(HtmlTextWriterTag.Tr);
16 for (int i = 0; i < columns.Length; i++)
17 {
18 writer.RenderBeginTag(HtmlTextWriterTag.Td);
19 value = typeof(T).GetProperty(columns[i]).GetValue(item, null).ToString();
20 if (i == 0)
21 {
22 writer.AddAttribute("type", "hidden");
23 writer.AddAttribute("id",items.IdentityName);
24 writer.AddAttribute("name", items.IdentityName);
25 writer.AddAttribute("value", typeof(T).GetProperty(items.IdentityName).GetValue(item, null).ToString());
26 writer.RenderBeginTag(HtmlTextWriterTag.Input);
27 writer.RenderEndTag();
28 writer.Write(value.Length > 20 ? value.Substring(0, 20) : value);
29 }
30 else
31 {
32 writer.Write(value.Length > 20 ? value.Substring(0, 20) : value);
33 }
34 //writer.Write(value);
35 writer.RenderEndTag();
36 }
37 if (option.IsEidt || option.IsDelete)
38 {
39 writer.RenderBeginTag(HtmlTextWriterTag.Td);
40 if (option.IsEidt)
41 {
42 //writer.Write(helper.ActionLink(option.EditButton, null, null, new { @class = "Edit_Link" }) + " ");
43 writer.Write("<a href='#' class='Edit_Link'>编辑</a> ");
44 }
45 if (option.IsDelete)
46 {
47 writer.Write(helper.ActionLink(option.DeleteButton, option.DeleteAction, new { controller = option.DeleteController, id = typeof(T).GetProperty(items.IdentityName).GetValue(item, null).ToString() }));
48 }
49
50 writer.RenderEndTag();
51 }
52 writer.RenderEndTag();
53 }
54 return writer.InnerWriter.ToString();
55 }
56
这一部分关键是从集合中将数据取出来 然后以表格的形式显示。在这里思维上不会有太大的难度,根据反射的属性来获取集合中的数据,另外要注意判断编辑和删按钮的判断,根据传递的参数的不同,来控制是否显示编辑或删除按钮
3.输出分页控件
1 /// <summary>
2 /// 添加分页
3 /// </summary>
4 /// <param name="helper"></param>
5 /// <param name="writer"></param>
6 /// <param name="option"></param>
7 /// <returns></returns>
8 public static string RenderPageList<T>(this HtmlHelper helper, HtmlTextWriter writer, PageList<T> items, string[] columns, GridViewOption option)
9 {
10 writer.RenderBeginTag(HtmlTextWriterTag.Tr);
11 if (option.IsEidt || option.IsDelete)
12 {
13 writer.AddAttribute("colspan", (option.Headers.Length + 1).ToString());
14 }
15 else
16 {
17 writer.AddAttribute("colspan", (option.Headers.Length).ToString());
18 }
19 writer.AddAttribute("align", "center");
20 writer.RenderBeginTag(HtmlTextWriterTag.Td);
21 writer.Write(helper.ActionLink("首页", items.Action, new { controller = items.Controller, pageIndex = 1 }) + " ");
22 writer.Write(helper.ActionLink("上一页", items.Action, new { controller = items.Controller, pageIndex = items.Page.PageIndex - 1 }) + " ");
23 writer.Write(items.Page.PageIndex + "/" + items.Page.PageCount + "页 ");
24 writer.Write(helper.ActionLink("下一页", items.Action, new { controller = items.Controller, pageIndex = items.Page.PageIndex + 1 }) + " ");
25 writer.Write(helper.ActionLink("尾页", items.Action, new { controller = items.Controller, pageIndex = items.Page.PageCount }) + " ");
26 writer.RenderEndTag();
27 writer.RenderEndTag();
28 writer.RenderEndTag();
29
30 //构建隐藏表单
31 writer.AddStyleAttribute("display","none");
32 writer.RenderBeginTag(HtmlTextWriterTag.Div);
33 writer.AddAttribute("action","");
34 writer.AddAttribute("method","post");
35 writer.RenderBeginTag(HtmlTextWriterTag.Form);
36 foreach (string column in columns)
37 {
38 writer.AddAttribute("type","hidden");
39 writer.AddAttribute("id", column);
40 writer.AddAttribute("name", column);
41 writer.AddAttribute("value", "");
42 writer.RenderBeginTag(HtmlTextWriterTag.Input);
43 writer.RenderEndTag();
44 }
45 writer.RenderEndTag();
46 writer.RenderEndTag();
47
48 return writer.InnerWriter.ToString();
49 }
这里的分页显示,其实同上面没有什么不同,都是将标签输出,然后加上不停变化的变量参数,如同编辑和删除按钮一样,要指定处理这个请求的Controller 和 action。同样的道理,分页中的分页参数也是如此。但是这里得注意一点,在最后面输出了一个表单,其实这个表单就是为了辅助编辑这个按钮的,当我编辑的时候,后来要将数据提交到后台做修改就是利用这个表单,这个表单都是用表单隐藏域来说实现数据的提交同时结合了Jquery中的相关知识内容。在域WevForm中的GridView比较起来这种方式都是在客户端处理的,编辑不需要从新走服务器,个人感觉这是比较好的一种方式,只有真正的修改了才会提交到服务器修改。隐藏域的个数也是由显示字段的个数决定的,从某种程度上来说,MVC这种表单提交方式,自动封装对象正好为此提供了方便。