asp.net MVC进阶学习---HtmlHelper之GridView控件拓展(四)

发布时间:2020-06-17编辑:脚本学堂
asp.net MVC进阶学习---HtmlHelper之GridView控件拓展(四)

在网上看到的一个不错的asp.net MVC学习系列,希望可以帮助那些正在研究MVC的朋友们。
本部分内容:asp.net MVC进阶学习---HtmlHelper之GridView控件拓展(四)

1.输出表头
  

复制代码 代码如下:
/// <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();
         }

    这个方法主要适用于添加表头的。这里我们常用的一个类,用于从后台输出标签的类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.添加数据行
 

复制代码 代码如下:
/// <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>&nbsp;&nbsp;&nbsp;");
                     }
                     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();
         }
 

    这一部分关键是从集合中将数据取出来 然后以表格的形式显示。在这里思维上不会有太大的难度,根据反射的属性来获取集合中的数据,另外要注意判断编辑和删按钮的判断,根据传递的参数的不同,来控制是否显示编辑或删除按钮。

3.输出分页控件
 

复制代码 代码如下:
/// <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 }) + "&nbsp;&nbsp;&nbsp;&nbsp;");
             writer.Write(helper.ActionLink("上一页", items.Action, new { controller = items.Controller, pageIndex = items.Page.PageIndex - 1 }) + "&nbsp;&nbsp;&nbsp;&nbsp;");
             writer.Write(items.Page.PageIndex + "/" + items.Page.PageCount + "页&nbsp;&nbsp;&nbsp;&nbsp;");
             writer.Write(helper.ActionLink("下一页", items.Action, new { controller = items.Controller, pageIndex = items.Page.PageIndex + 1 }) + "&nbsp;&nbsp;&nbsp;&nbsp;");
             writer.Write(helper.ActionLink("尾页", items.Action, new { controller = items.Controller, pageIndex = items.Page.PageCount }) + "&nbsp;&nbsp;&nbsp;&nbsp;");
             writer.RenderEndTag();
             writer.RenderEndTag();
             writer.RenderEndTag();
 
             //构建隐藏表单
             writer.AddStyleAttribute("display","none");
             writer.RenderBeginTag(HtmlTextWriterTag.Div);
             writer.AddAttribute("action","");
             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();
         }

    这里的分页显示,其实同上面没有什么不同,都是将标签输出,然后加上不停变化的变量参数,如同编辑和删除按钮一样,要指定处理这个请求的Controller 和 action。同样的道理,分页中的分页参数也是如此。但是这里得注意一点,在最后面输出了一个表单,其实这个表单就是为了辅助编辑这个按钮的,当我编辑的时候,后来要将数据提交到后台做修改就是利用这个表单,这个表单都是用表单隐藏域来说实现数据的提交同时结合了jquery中的相关知识内容。在域WevForm中的GridView比较起来这种方式都是在客户端处理的,编辑不需要从新走服务器,个人感觉这是比较好的一种方式,只有真正的修改了才会提交到服务器修改。隐藏域的个数也是由显示字段的个数决定的,从某种程度上来说,MVC这种表单提交方式,自动封装对象正好为此提供了方便。

原文作者:情缘 http://www.cnblogs.com/qingyuan