在网上看到的一个不错的asp.net MVC学习系列,希望可以帮助那些正在研究MVC的朋友们。
本部分内容:asp.net MVC进阶学习---HtmlHelper之GridView控件拓展(三) 。
1.扩展核心代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc.Html;
using System.Web.Mvc;
using MvcTest.Code;
using System.Web.UI;
using System.IO;
using System.Data;
namespace MvcTest.Code
{
public static class GridViewHelper
{
public static string GridView<T>(this HtmlHelper helper, PageList<T> items, string[] columns, GridViewOption option)
{
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;
}
}
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(); ;
}
/// <summary>
/// 添加表头行
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="helper"></param>
/// <param name="writer"></param>
/// <param name="columns"></param>
/// <param name="option"></param>
/// <returns></returns>
public static string RenderHeander<T>(this HtmlHelper helper, HtmlTextWriter writer, string[] columns, GridViewOption option)
{
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
for (int i = 0; i < option.Headers.Length;i++ )
{
writer.AddAttribute("id",columns[i]);
writer.AddAttribute("name", columns[i]);
writer.RenderBeginTag(HtmlTextWriterTag.Th);
writer.Write(option.Headers[i]);
writer.RenderEndTag();
}
//如果有编辑或删除列,则要在原有列的基础上加一列
if (option.IsEidt || option.IsDelete)
{
writer.RenderBeginTag(HtmlTextWriterTag.Th);
writer.RenderEndTag();
}
writer.RenderEndTag();
return writer.InnerWriter.ToString();
}
/// <summary>
/// 添加数据行
/// </summary>
/// <param name="helper"></param>
/// <param name="writer"></param>
/// <param name="item"></param>
/// <param name="columns"></param>
/// <param name="option"></param>
/// <returns></returns>
public static string RenderRow<T>(this HtmlHelper helper, HtmlTextWriter writer, PageList<T> items, string[] columns, GridViewOption option)
{
string value = null;
foreach (var item in items)
{
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
for (int i = 0; i < columns.Length; i++)
{
writer.RenderBeginTag(HtmlTextWriterTag.Td);
value = typeof(T).GetProperty(columns[i]).GetValue(item, null).ToString();
if (i == 0)
{
writer.AddAttribute("type", "hidden");
writer.AddAttribute("id",items.IdentityName);
writer.AddAttribute("name", items.IdentityName);
writer.AddAttribute("value", typeof(T).GetProperty(items.IdentityName).GetValue(item, null).ToString());
writer.RenderBeginTag(HtmlTextWriterTag.Input);
writer.RenderEndTag();
writer.Write(value.Length > 20 ? value.Substring(0, 20) : value);
}
else
{
writer.Write(value.Length > 20 ? value.Substring(0, 20) : value);
}
//writer.Write(value);
writer.RenderEndTag();
}
if (option.IsEidt || option.IsDelete)
{
writer.RenderBeginTag(HtmlTextWriterTag.Td);
if (option.IsEidt)
{
//writer.Write(helper.ActionLink(option.EditButton, null, null, new { @class = "Edit_Link" }) + " ");
writer.Write("<a href='#' class='Edit_Link'>编辑</a> ");
}
if (option.IsDelete)
{
writer.Write(helper.ActionLink(option.DeleteButton, option.DeleteAction, new { controller = option.DeleteController, id = typeof(T).GetProperty(items.IdentityName).GetValue(item, null).ToString() }));
}
writer.RenderEndTag();
}
writer.RenderEndTag();
}
return writer.InnerWriter.ToString();
}
/// <summary>
/// 添加分页
/// </summary>
/// <param name="helper"></param>
/// <param name="writer"></param>
/// <param name="option"></param>
/// <returns></returns>
public static string RenderPageList<T>(this HtmlHelper helper, HtmlTextWriter writer, PageList<T> items, string[] columns, GridViewOption option)
{
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
if (option.IsEidt || option.IsDelete)
{
writer.AddAttribute("colspan", (option.Headers.Length + 1).ToString());
}
else
{
writer.AddAttribute("colspan", (option.Headers.Length).ToString());
}
writer.AddAttribute("align", "center");
writer.RenderBeginTag(HtmlTextWriterTag.Td);
writer.Write(helper.ActionLink("首页", items.Action, new { controller = items.Controller, pageIndex = 1 }) + " ");
writer.Write(helper.ActionLink("上一页", items.Action, new { controller = items.Controller, pageIndex = items.Page.PageIndex - 1 }) + " ");
writer.Write(items.Page.PageIndex + "/" + items.Page.PageCount + "页 ");
writer.Write(helper.ActionLink("下一页", items.Action, new { controller = items.Controller, pageIndex = items.Page.PageIndex + 1 }) + " ");
writer.Write(helper.ActionLink("尾页", items.Action, new { controller = items.Controller, pageIndex = items.Page.PageCount }) + " ");
writer.RenderEndTag();
writer.RenderEndTag();
writer.RenderEndTag();
//构建隐藏表单
writer.AddStyleAttribute("display","none");
writer.RenderBeginTag(HtmlTextWriterTag.Div);
writer.AddAttribute("id","form0");
writer.AddAttribute("action","/Home/Update");
writer.AddAttribute("method","post");
writer.RenderBeginTag(HtmlTextWriterTag.Form);
foreach (string column in columns)
{
writer.AddAttribute("type","hidden");
writer.AddAttribute("id", column);
writer.AddAttribute("name", column);
writer.AddAttribute("value", "");
writer.RenderBeginTag(HtmlTextWriterTag.Input);
writer.RenderEndTag();
}
writer.RenderEndTag();
writer.RenderEndTag();
return writer.InnerWriter.ToString();
}
}
}
注解:
(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(); ;
这段就是调用下面的方法,设置数据。更多方法介绍看下篇。
原文作者:情缘 http://www.cnblogs.com/qingyuan