在这里我们将谈到的是ASP.NET MVC框架中的一个HTML辅助类,希望通过本文能对大家了解ASP.NET MVC分页有所帮助。
#T#
本人写的一个ASP.NET MVC分页Helper,支持普通分页(也就是,首页、上一页、下一页、末页等),综合分页(普通分页和数字分页的综合)。下面是ASP.NET MVC分页效果:
分页代码:
PagerHelper.cs
--> 1 using System;
2 using System.Collections.Generic;
3 using System.Collections.Specialized;
4 using System.Linq;
5 using System.Web;
6 using System.Text;
7 using System.Web.Mvc;
8 using System.Web.Routing;
9 using System.Data.Objects.DataClasses;
10 namespace System.Web.Mvc
11 {
12 public static class PagerHelper
13 {
14 ///
15 /// 分页
16 ///
17 ///
18 /// 分页id
19 /// 当前页
20 /// 分页尺寸
21 /// 记录总数
22 /// 分页头标签属性
23 /// 分页样式
24 /// 分页模式
25 ///
26 public static string Pager(this HtmlHelper helper, string id, int currentPageIndex,
27 {
28 TagBuilder builder = new TagBuilder("table");
29 builder.IdAttributeDotReplacement = "_";
30 builder.GenerateId(id);
31 builder.AddCssClass(className);
32 builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
33 builder.InnerHtml = GetNormalPage(currentPageIndex, pageSize, recordCount,mode);
34 return builder.ToString();
35 }
36 ///
37 /// 分页
38 ///
39 ///
40 /// 分页id
41 /// 当前页
42 /// 分页尺寸
43 /// 记录总数
44 /// 分页样式
45 ///
46 public static string Pager(this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount, string className)
47 {
48 return Pager(helper, id, currentPageIndex, pageSize, recordCount, null, className,PageMode.Normal);
49 }
50 ///
51 /// 分页
52 ///
53 ///
54 /// 分页id
55 /// 当前页
56 /// 分页尺寸
57 /// 记录总数
58 ///
59 public static string Pager(this HtmlHelper helper,string id,int currentPageIndex,int pageSize,int recordCount)
60 {
61 return Pager(helper, id, currentPageIndex, pageSize, recordCount,null);
62 }
63 ///
64 /// 分页
65 ///
66 ///
67 /// 分页id
68 /// 当前页
69 /// 分页尺寸
70 /// 记录总数
71 /// 分页模式
72 ///
73 public static string Pager(this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount,PageMode mode)
74 {
75 return Pager(helper, id, currentPageIndex, pageSize, recordCount, null,mode);
76 }
77 ///
78 /// 分页
79 ///
80 ///
81 /// 分页id
82 /// 当前页
83 /// 分页尺寸
84 /// 记录总数
85 /// 分页样式
86 /// 分页模式
87 ///
88 public static string Pager(this HtmlHelper helper, string id, i
89 {
90 return Pager(helper, id, currentPageIndex, pageSize, recordCount, null,className,mode);
91 }
92 ///
93 /// 获取普通分页
94 ///
95 ///
96 ///
97 ///
98 ///
99 private static string GetNormalPage(int currentPageIndex, int pageSize, int recordCount,PageMode mode)
100 {
101 int pageCount = (recordCount%pageSize ==0?recordCount/pageSize:recordCount/pageSize+1);
102 StringBuilder url = new StringBuilder();
103 url.Append(HttpContext.Current.Request.Url.AbsolutePath+"?page={0}");
104 NameValueCollection collection = HttpContext.Current.Request.QueryString;
105 string[] keys = collection.AllKeys;
106 for (int i = 0; i < keys.Length; i++)
107 {
108 if (keys[i].ToLower() != "page")
109 url.AppendFormat("&{0}={1}", keys[i], collection[keys[i]]);
110 }
111 StringBuilder sb = new StringBuilder();
112 sb.Append("
113 sb.AppendFormat("总共{0}条记录,共{1}页,当前第{2}页 ", recordCount, pageCount, currentPageIndex);
114 if (currentPageIndex == 1)
115 sb.Append("首页 ");
116 else
117 {
118 string url1 = string.Format(url.ToString(), 1);
119 sb.AppendFormat("首页 ", url1);
120 }
121 if (currentPageIndex > 1)
122 {
123 string url1 = string.Format(url.ToString(), currentPageIndex - 1);
124 sb.AppendFormat("上一页 ", url1);
125 }
126 else
127 sb.Append("上一页 ");
128 if(mode == PageMode.Numeric)
129 sb.Append(GetNumericPage(currentPageIndex,pageSize,recordCount,pageCount,url.ToString()));
130 if (currentPageIndex < pageCount)
131 {
132 string url1 = string.Format(url.ToString(), currentPageIndex+1);
133 sb.AppendFormat("下一页 ", url1);
134 }
135 else
136 sb.Append("下一页 ");
137
138 if (currentPageIndex == pageCount)
139 sb.Append("末页 ");
140 else
141 {
142 string url1 = string.Format(url.ToString(), pageCount);
143 sb.AppendFormat("末页 ", url1);
144 }
145 return sb.ToString();
146 }
147 ///
148 /// 获取数字分页
149 ///
150 ///
151 ///
152 ///
153 ///
154 ///
155 ///
156 private static string GetNumericPage(int currentPageIndex, int pageSize, int recordCount, int pageCount,string url)
157 {
158 int k = currentPageIndex / 10;
159 int m = currentPageIndex % 10;
160 StringBuilder sb = new StringBuilder();
161 if (currentPageIndex / 10 == pageCount / 10)
162 {
163 if (m == 0)
164 {
165 k--;
166 m = 10;
167 }
168 else
169 m = pageCount%10;
170 }
171 else
172 m = 10;
173 for (int i = k * 10 + 1; i <= k * 10 + m; i++)
174 {
175 if (i == currentPageIndex)
176 sb.AppendFormat("{0} ", i);
177 else
178 {
179 string url1 = string.Format(url.ToString(), i);
180 sb.AppendFormat("{1} ",url1, i);
181 }
182 }
183
184 return sb.ToString();
185 }
186 }
187 ///
188 /// 分页模式
189 ///
190 public enum PageMode
191 {
192 ///
193 /// 普通分页模式
194 ///
195 Normal,
196 ///
197 /// 普通分页加数字分页
198 ///
199 Numeric
200 }
201 }
202
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace System.Web.Mvc
- {
- public class PagerQuery<TPager,TEntityList>
- {
- public PagerQuery(TPager pager, TEntityList entityList)
- {
- this.Pager = pager;
- this.EntityList = entityList;
- }
- public TPager Pager { get; set; }
- public TEntityList EntityList { get; set; }
- }
- }
代码
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace System.Web.Mvc
- {
- public class PagerInfo
- {
- public int RecordCount { get; set; }
- public int CurrentPageIndex { get; set; }
- public int PageSize { get; set; }
- }
- }
使用示例:
代码
- <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
- Inherits="System.Web.Mvc.ViewPage<PagerQuery<PagerInfo, IList<NewsArticleInfo>>>" %>
- <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
- NewsList
- </asp:Content>
- <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
- <h2>NewsList</h2>
- <table> <tr> <th></th>
- <th>NoteID</th>
- <th>Title</th>
- <th>Author</th>
- <th>Hit</th>
- <th>ReplyNum</th>
- </tr>
- <% foreach (var item in Model.EntityList) { %>
- <tr><td>
- <%= Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) %> |
- <%= Html.ActionLink("Details", "NewsDetail", new { noteID=item.NoteID })%>
- </td><td>
- <%= Html.Encode(item.NoteID) %>
- </td>
- <td><%= Html.Encode(item.Title) %></td>
- <td><%= Html.Encode(item.Author)%></td>
- <td><%= Html.Encode(item.Hit)%></td>
- <td><%= Html.Encode(item.ReplyNum)%></td>
- </tr>
- <% } %></table>
- <p>
- <%=Html.Pager("pager",Model.Pager.CurrentPageIndex,Model.Pager.PageSize,Model.Pager.RecordCount,PageMode.Numeric) %></p>
- </asp:Content>
controler:
- [AcceptVerbs(HttpVerbs.Get)]
- ublic ActionResult NewsList(int boardID,int? page)
- {
- PagerInfo pager = new PagerInfo();
- NewsArticleInfo info = new NewsArticleInfo();
- info.NewsBoard = new NewsBoardInfo();
- info.NewsBoard.BoardID = boardID;
- pager.RecordCount = Resolve
().GetArticleDataList(info, ArticleTypeEnum.Pass); - pager.PageSize = 10;
- pager.CurrentPageIndex = (page!=null?(int)page:1);
- IList
result = Resolve ().GetArticleDataList(pager.CurrentPageIndex, pager.PageSize, ArticleTypeEnum.Pass, info); - PagerQuery> query = new PagerQuery>(pager,result);
- return View(query);
原文标题:一个MVC分页Helper
链接:http://www.cnblogs.com/JackFeng/archive/2010/01/25/JackFeng.html
责任编辑:彭凡 来源: 博客园 相关推荐2009-07-28 14:47:18
2009-09-18 10:20:26
2009-07-22 13:24:24
2009-07-20 10:53:59
2009-07-22 10:09:59
2009-07-23 14:31:20
2011-04-14 09:19:22