本人写的一个分页Helper,支持普通分页(也就是,首页、上一页、下一页、末页等),综合分页(普通分页和数字分页的综合)。下面是分页效果:
分页代码:
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 /// <summary>
15 /// 分页
16 /// </summary>
17 /// <param name="helper"></param>
18 /// <param name="id">分页id</param>
19 /// <param name="currentPageIndex">当前页</param>
20 /// <param name="pageSize">分页尺寸</param>
21 /// <param name="recordCount">记录总数</param>
22 /// <param name="htmlAttributes">分页头标签属性</param>
23 /// <param name="className">分页样式</param>
24 /// <param name="mode">分页模式</param>
25 /// <returns></returns>
26 public static string Pager(this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount, object htmlAttributes, string className,PageMode mode)
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 /// <summary>
37 /// 分页
38 /// </summary>
39 /// <param name="helper"></param>
40 /// <param name="id">分页id</param>
41 /// <param name="currentPageIndex">当前页</param>
42 /// <param name="pageSize">分页尺寸</param>
43 /// <param name="recordCount">记录总数</param>
44 /// <param name="className">分页样式</param>
45 /// <returns></returns>
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 /// <summary>
51 /// 分页
52 /// </summary>
53 /// <param name="helper"></param>
54 /// <param name="id">分页id</param>
55 /// <param name="currentPageIndex">当前页</param>
56 /// <param name="pageSize">分页尺寸</param>
57 /// <param name="recordCount">记录总数</param>
58 /// <returns></returns>
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 /// <summary>
64 /// 分页
65 /// </summary>
66 /// <param name="helper"></param>
67 /// <param name="id">分页id</param>
68 /// <param name="currentPageIndex">当前页</param>
69 /// <param name="pageSize">分页尺寸</param>
70 /// <param name="recordCount">记录总数</param>
71 /// <param name="mode">分页模式</param>
72 /// <returns></returns>
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 /// <summary>
78 /// 分页
79 /// </summary>
80 /// <param name="helper"></param>
81 /// <param name="id">分页id</param>
82 /// <param name="currentPageIndex">当前页</param>
83 /// <param name="pageSize">分页尺寸</param>
84 /// <param name="recordCount">记录总数</param>
85 /// <param name="className">分页样式</param>
86 /// <param name="mode">分页模式</param>
87 /// <returns></returns>
88 public static string Pager(this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount,string className, PageMode mode)
89 {
90 return Pager(helper, id, currentPageIndex, pageSize, recordCount, null,className,mode);
91 }
92 /// <summary>
93 /// 获取普通分页
94 /// </summary>
95 /// <param name="currentPageIndex"></param>
96 /// <param name="pageSize"></param>
97 /// <param name="recordCount"></param>
98 /// <returns></returns>
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("<tr><td>");
113 sb.AppendFormat("总共{0}条记录,共{1}页,当前第{2}页 ", recordCount, pageCount, currentPageIndex);
114 if (currentPageIndex == 1)
115 sb.Append("<span>首页</span> ");
116 else
117 {
118 string url1 = string.Format(url.ToString(), 1);
119 sb.AppendFormat("<span><a href={0}>首页</a></span> ", url1);
120 }
121 if (currentPageIndex > 1)
122 {
123 string url1 = string.Format(url.ToString(), currentPageIndex - 1);
124 sb.AppendFormat("<span><a href={0}>上一页</a></span> ", url1);
125 }
126 else
127 sb.Append("<span>上一页</span> ");
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("<span><a href={0}>下一页</a></span> ", url1);
134 }
135 else
136 sb.Append("<span>下一页</span> ");
137
138 if (currentPageIndex == pageCount)
139 sb.Append("<span>末页</span> ");
140 else
141 {
142 string url1 = string.Format(url.ToString(), pageCount);
143 sb.AppendFormat("<span><a href={0}>末页</a></span> ", url1);
144 }
145 return sb.ToString();
146 }
147 /// <summary>
148 /// 获取数字分页
149 /// </summary>
150 /// <param name="currentPageIndex"></param>
151 /// <param name="pageSize"></param>
152 /// <param name="recordCount"></param>
153 /// <param name="pageCount"></param>
154 /// <param name="url"></param>
155 /// <returns></returns>
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("<span><font color=red><b>{0}</b></font></span> ", i);
177 else
178 {
179 string url1 = string.Format(url.ToString(), i);
180 sb.AppendFormat("<span><a href={0}>{1}</a></span> ",url1, i);
181 }
182 }
183
184 return sb.ToString();
185 }
186 }
187 /// <summary>
188 /// 分页模式
189 /// </summary>
190 public enum PageMode
191 {
192 /// <summary>
193 /// 普通分页模式
194 /// </summary>
195 Normal,
196 /// <summary>
197 /// 普通分页加数字分页
198 /// </summary>
199 Numeric
200 }
201 }
202
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 /// <summary>
15 /// 分页
16 /// </summary>
17 /// <param name="helper"></param>
18 /// <param name="id">分页id</param>
19 /// <param name="currentPageIndex">当前页</param>
20 /// <param name="pageSize">分页尺寸</param>
21 /// <param name="recordCount">记录总数</param>
22 /// <param name="htmlAttributes">分页头标签属性</param>
23 /// <param name="className">分页样式</param>
24 /// <param name="mode">分页模式</param>
25 /// <returns></returns>
26 public static string Pager(this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount, object htmlAttributes, string className,PageMode mode)
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 /// <summary>
37 /// 分页
38 /// </summary>
39 /// <param name="helper"></param>
40 /// <param name="id">分页id</param>
41 /// <param name="currentPageIndex">当前页</param>
42 /// <param name="pageSize">分页尺寸</param>
43 /// <param name="recordCount">记录总数</param>
44 /// <param name="className">分页样式</param>
45 /// <returns></returns>
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 /// <summary>
51 /// 分页
52 /// </summary>
53 /// <param name="helper"></param>
54 /// <param name="id">分页id</param>
55 /// <param name="currentPageIndex">当前页</param>
56 /// <param name="pageSize">分页尺寸</param>
57 /// <param name="recordCount">记录总数</param>
58 /// <returns></returns>
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 /// <summary>
64 /// 分页
65 /// </summary>
66 /// <param name="helper"></param>
67 /// <param name="id">分页id</param>
68 /// <param name="currentPageIndex">当前页</param>
69 /// <param name="pageSize">分页尺寸</param>
70 /// <param name="recordCount">记录总数</param>
71 /// <param name="mode">分页模式</param>
72 /// <returns></returns>
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 /// <summary>
78 /// 分页
79 /// </summary>
80 /// <param name="helper"></param>
81 /// <param name="id">分页id</param>
82 /// <param name="currentPageIndex">当前页</param>
83 /// <param name="pageSize">分页尺寸</param>
84 /// <param name="recordCount">记录总数</param>
85 /// <param name="className">分页样式</param>
86 /// <param name="mode">分页模式</param>
87 /// <returns></returns>
88 public static string Pager(this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount,string className, PageMode mode)
89 {
90 return Pager(helper, id, currentPageIndex, pageSize, recordCount, null,className,mode);
91 }
92 /// <summary>
93 /// 获取普通分页
94 /// </summary>
95 /// <param name="currentPageIndex"></param>
96 /// <param name="pageSize"></param>
97 /// <param name="recordCount"></param>
98 /// <returns></returns>
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("<tr><td>");
113 sb.AppendFormat("总共{0}条记录,共{1}页,当前第{2}页 ", recordCount, pageCount, currentPageIndex);
114 if (currentPageIndex == 1)
115 sb.Append("<span>首页</span> ");
116 else
117 {
118 string url1 = string.Format(url.ToString(), 1);
119 sb.AppendFormat("<span><a href={0}>首页</a></span> ", url1);
120 }
121 if (currentPageIndex > 1)
122 {
123 string url1 = string.Format(url.ToString(), currentPageIndex - 1);
124 sb.AppendFormat("<span><a href={0}>上一页</a></span> ", url1);
125 }
126 else
127 sb.Append("<span>上一页</span> ");
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("<span><a href={0}>下一页</a></span> ", url1);
134 }
135 else
136 sb.Append("<span>下一页</span> ");
137
138 if (currentPageIndex == pageCount)
139 sb.Append("<span>末页</span> ");
140 else
141 {
142 string url1 = string.Format(url.ToString(), pageCount);
143 sb.AppendFormat("<span><a href={0}>末页</a></span> ", url1);
144 }
145 return sb.ToString();
146 }
147 /// <summary>
148 /// 获取数字分页
149 /// </summary>
150 /// <param name="currentPageIndex"></param>
151 /// <param name="pageSize"></param>
152 /// <param name="recordCount"></param>
153 /// <param name="pageCount"></param>
154 /// <param name="url"></param>
155 /// <returns></returns>
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("<span><font color=red><b>{0}</b></font></span> ", i);
177 else
178 {
179 string url1 = string.Format(url.ToString(), i);
180 sb.AppendFormat("<span><a href={0}>{1}</a></span> ",url1, i);
181 }
182 }
183
184 return sb.ToString();
185 }
186 }
187 /// <summary>
188 /// 分页模式
189 /// </summary>
190 public enum PageMode
191 {
192 /// <summary>
193 /// 普通分页模式
194 /// </summary>
195 Normal,
196 /// <summary>
197 /// 普通分页加数字分页
198 /// </summary>
199 Numeric
200 }
201 }
202
PagerQuery.cs包含两个属性,一个是PageInfo实体类属性Pager,包含RecordCount,CurrentPageIndex,PageSize三个属性。一个是Model EntityList属性。
代码
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5
6 namespace System.Web.Mvc
7 {
8 public class PagerQuery<TPager,TEntityList>
9 {
10 public PagerQuery(TPager pager, TEntityList entityList)
11 {
12 this.Pager = pager;
13 this.EntityList = entityList;
14 }
15 public TPager Pager { get; set; }
16 public TEntityList EntityList { get; set; }
17 }
18 }
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5
6 namespace System.Web.Mvc
7 {
8 public class PagerQuery<TPager,TEntityList>
9 {
10 public PagerQuery(TPager pager, TEntityList entityList)
11 {
12 this.Pager = pager;
13 this.EntityList = entityList;
14 }
15 public TPager Pager { get; set; }
16 public TEntityList EntityList { get; set; }
17 }
18 }
PageInfo.cs
代码
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5
6 namespace System.Web.Mvc
7 {
8 public class PagerInfo
9 {
10 public int RecordCount { get; set; }
11
12 public int CurrentPageIndex { get; set; }
13
14 public int PageSize { get; set; }
15 }
16 }
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5
6 namespace System.Web.Mvc
7 {
8 public class PagerInfo
9 {
10 public int RecordCount { get; set; }
11
12 public int CurrentPageIndex { get; set; }
13
14 public int PageSize { get; set; }
15 }
16 }
使用示例:
代码
<%@ 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>
<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:
代码
1 [AcceptVerbs(HttpVerbs.Get)]
2 public ActionResult NewsList(int boardID,int? page)
3 {
4 PagerInfo pager = new PagerInfo();
5 NewsArticleInfo info = new NewsArticleInfo();
6 info.NewsBoard = new NewsBoardInfo();
7 info.NewsBoard.BoardID = boardID;
8 pager.RecordCount = Resolve<INewsBLL>().GetArticleDataList(info, ArticleTypeEnum.Pass);
9 pager.PageSize = 10;
10 pager.CurrentPageIndex = (page!=null?(int)page:1);
11 IList<NewsArticleInfo> result = Resolve<INewsBLL>().GetArticleDataList(pager.CurrentPageIndex, pager.PageSize, ArticleTypeEnum.Pass, info);
12 PagerQuery<PagerInfo, IList<NewsArticleInfo>> query = new PagerQuery<PagerInfo, IList<NewsArticleInfo>>(pager,result);
13 return View(query);
14 }
2 public ActionResult NewsList(int boardID,int? page)
3 {
4 PagerInfo pager = new PagerInfo();
5 NewsArticleInfo info = new NewsArticleInfo();
6 info.NewsBoard = new NewsBoardInfo();
7 info.NewsBoard.BoardID = boardID;
8 pager.RecordCount = Resolve<INewsBLL>().GetArticleDataList(info, ArticleTypeEnum.Pass);
9 pager.PageSize = 10;
10 pager.CurrentPageIndex = (page!=null?(int)page:1);
11 IList<NewsArticleInfo> result = Resolve<INewsBLL>().GetArticleDataList(pager.CurrentPageIndex, pager.PageSize, ArticleTypeEnum.Pass, info);
12 PagerQuery<PagerInfo, IList<NewsArticleInfo>> query = new PagerQuery<PagerInfo, IList<NewsArticleInfo>>(pager,result);
13 return View(query);
14 }
2010年1月26日源码提供,欢迎下载: