);//显示设置页面后的链接
7: Writer.Write(" ");//连接后显示个空格,好看点
8:
9: }%>
这个列表,我们就可以显示为
3.4 完善这个Pager并封装成一个Helper
上面列出了 Pager,但是有几个问题
没有上下页
没有指定当前页的特殊显示
每次调用时都要写一次
如果QueryString有其它 参数时无法处理
那我们下面来完善这个Pager
并将之封装成一个Helper
1: /// <summary>
2: /// 分页Pager显示
3: /// </summary>
4: /// <param name="html"></param>
5: /// <param name="currentPageStr">标识当前页码的 QueryStringKey</param>
6: /// <param name="pageSize">每页显示</param>
7: /// <param name="totalCount">总数据量</param>
8: /// <returns></returns>
9: public static string Pager(this HtmlHelper html, string currentPageStr, int pageSize, int totalCount)
10: {
11: var queryString = html.ViewContext.HttpContext.Request.QueryString;
12: int currentPage = 1; //当前页
13: int.TryParse(queryString[currentPageStr], out currentPage); //与相应的QueryString绑定
14: var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1); //总页数
15: var dict = new RouteValueDictionary(html.ViewContext.RouteData.Values);
16:
17: var output = new StringBuilder ();
18:
19: foreach (string key in queryString.Keys)
20: if (queryString[key] != null && !string.IsNullOrEmpty(key))
21: dict[key] = queryString[key];
22: if (totalPages > 1)
23: {
24: if (currentPage != 1)
25: {//处理首页连接
26: dict["p"] = 1;
27: output.AppendFormat ("{0} ",html.RouteLink("首页", dict));
28: }
29: if (currentPage > 1)
30: {//处理上一页的连接
31: dict["p"] = currentPage - 1;
32: output.Append(html.RouteLink("上一页", dict));
33: }
34: else
35: {
36: output.Append("上一页 ");
37: }
38: output.Append(" ");
39: int currint = 5;
40: for (int i = 0; i <= 10; i++)
41: {//一共最多显示10个页 码,前面5个,后面5个
42: if ((currentPage + i - currint) >= 1 && (currentPage + i - currint) <= totalPages)
43: if (currint == i)
44: {// 当前页处理
45: output.Append(string.Format("[{0}]", currentPage));
46: }
47: else
48:
|