一个asp.net MVC 的分页代码

简介: 哈哈,这个博客的处女文啦~~ 首先声明,这个分页代码并不是出自我手哈,借用了网上的一段代码,然后加了个css,变的好看一些啦~~ 原作者忘记是谁了,实在找不到了……万分抱歉啊~~ 效果如下:     1 using System; 2  using System.

哈哈,这个博客的处女文啦~~

首先声明,这个分页代码并不是出自我手哈,借用了网上的一段代码,然后加了个css,变的好看一些啦~~

原作者忘记是谁了,实在找不到了……万分抱歉啊~~

效果如下:

 

 

img_405b18b4b6584ae338e0f6ecaf736533.gif
 
   
1 using System;
2   using System.Collections.Generic;
3   using System.Linq;
4   using System.Web;
5   using System.Web.Mvc;
6   using System.Web.Routing;
7   using System.Text;
8   using System.Web.Mvc.Html;
9   namespace application.Helpers
10 {
11 public static class PagerExtensions
12 {
13 /* helper分页
14 *使用方式:<%@ Import Namespace="ExpoShiep.Helpers" %>
15 *<%=Html.Pager("page",10,100)%> */
16 /// < summary>
17 /// 分页Pager显示
18 /// < /summary>
19 /// < param name="html">< /param>
20 /// < param name="currentPageStr"> 标识当前页码的QueryStringKey < /param>
21 /// < param name="pageSize"> 每页显示 < /param>
22 /// < param name="totalCount"> 总数据量 < /param>
23 /// < returns>< /returns>
24   public static string Pager( this HtmlHelper html, string currentPageStr, int pageSize, int totalCount)
25 {
26 var queryString = html.ViewContext.HttpContext.Request.QueryString;
27 int currentPage = 1 ; // 当前页
28   var totalPages = Math.Max((totalCount + pageSize - 1 ) / pageSize, 1 ); // 总页数
29   var dict = new System.Web.Routing.RouteValueDictionary(html.ViewContext.RouteData.Values);
30 var output = new System.Text.StringBuilder();
31 if ( ! string .IsNullOrEmpty(queryString[currentPageStr]))
32 { // 与相应的QueryString绑定
33 foreach ( string key in queryString.Keys)
34 if (queryString[key] != null && ! string .IsNullOrEmpty(key))
35 dict[key] = queryString[key];
36 int .TryParse(queryString[currentPageStr], out currentPage);
37 }
38 else
39 { // 获取 ~/Page/{page number} 的页号参数
40 int .TryParse(dict[currentPageStr].ToString(), out currentPage);
41 }
42 if (currentPage <= 0 )
43 currentPage = 1 ;
44 if (totalPages > 1 )
45 {
46 if (currentPage != 1 )
47 { // 处理首页连接
48 dict[currentPageStr] = 1 ;
49 output.AppendFormat( " {0} " , html.RouteLink( " 首页 " , dict));
50 }
51 if (currentPage > 1 )
52 { // 处理上一页的连接
53 dict[currentPageStr] = currentPage - 1 ;
54 output.Append(html.RouteLink( " 上一页 " , dict));
55 }
56 else
57 {
58 // output.Append("上一页");
59 dict[currentPageStr] = currentPage;
60 output.Append(html.RouteLink( " 上一页 " , dict));
61 }
62 output.Append( " " );
63 int currint = 5 ;
64 for ( int i = 0 ; i <= 10 ; i ++ )
65 { // 一共最多显示10个页码,前面5个,后面5个
66 if ((currentPage + i - currint) >= 1 && (currentPage + i - currint) <= totalPages)
67 if (currint == i)
68 { // 当前页处理
69 // output.Append(string.Format("[{0}]", currentPage));
70 dict[currentPageStr] = currentPage;
71 output.AppendFormat( " <span class=\ " current\ " >{0}</span> " , currentPage);
72 }
73 else
74 {
75 // 一般页处理
76 dict[currentPageStr] = currentPage + i - currint;
77 output.Append(html.RouteLink((currentPage + i - currint).ToString(), dict));
78 }
79 output.Append( " " );
80 }
81 if (currentPage < totalPages)
82 {
83 // 处理下一页的链接
84 dict[currentPageStr] = currentPage + 1 ;
85 output.Append(html.RouteLink( " 下一页 " , dict));
86 }
87 else
88 {
89 // output.Append("下一页");
90 dict[currentPageStr] = currentPage;
91 output.Append(html.RouteLink( " 下一页 " , dict));
92 }
93 output.Append( " " );
94 if (currentPage != totalPages)
95 {
96 dict[currentPageStr] = totalPages;
97 output.Append(html.RouteLink( " 末页 " , dict));
98 }
99 output.Append( " " );
100 }
101 output.AppendFormat( " {0} / {1} " , currentPage, totalPages); // 这个统计加不加都行
102 return output.ToString();
103 }
104 }
105 }

 

CSS:

 

img_405b18b4b6584ae338e0f6ecaf736533.gif
 
   
1 .pager
2 {
3 font - size: 12px;
4 margin: 8px 0 ;
5 padding: 3px 0 3px;
6 text - align: left;
7 }
8
9 .pager .current
10 {
11 background - color: #06c;
12 border: 1px solid # 009 ;
13 color: #fff;
14 font - weight: bold;
15 margin - top: 4px;
16 padding: 3px 5px;
17 text - align: center;
18 }
19
20 .pager a
21 {
22 margin: 4px 3px;
23 border: 1px solid #9AAFE5;
24 padding: 3px 5px;
25 text - align: center;
26 text - decoration: none;
27 color: #2E6AB1;
28 }
29
30 .pager .pagerInput
31 {
32 padding: 3px 0 0 0 ;
33 border: 1px solid #9AAFE5;
34 text - align: center;
35 text - decoration: none;
36 height: 16px;
37 width: 30px;
38 }
39 .pager .pagerButton
40 {
41 border: 1px solid #9AAFE5;
42 cursor: pointer;
43 }
44

 

 

View中使用:

 

 
  
1 < div class = " pager " >
2 <%= Html.Pager( " page " , 10 , 300 )) %>
3 </ div >

 

目录
相关文章
|
开发框架 .NET PHP
ASP.NET Web Pages - 添加 Razor 代码
ASP.NET Web Pages 使用 Razor 标记添加服务器端代码,支持 C# 和 Visual Basic。Razor 语法简洁易学,类似于 ASP 和 PHP。例如,在网页中加入 `@DateTime.Now` 可以实时显示当前时间。
|
开发框架 前端开发 JavaScript
ASP.NET MVC 教程
ASP.NET 是一个使用 HTML、CSS、JavaScript 和服务器脚本创建网页和网站的开发框架。
460 7
|
存储 开发框架 前端开发
ASP.NET MVC 迅速集成 SignalR
ASP.NET MVC 迅速集成 SignalR
384 0
|
开发框架 前端开发 .NET
ASP.NET MVC WebApi 接口返回 JOSN 日期格式化 date format
ASP.NET MVC WebApi 接口返回 JOSN 日期格式化 date format
385 0
|
开发框架 前端开发 安全
ASP.NET MVC 如何使用 Form Authentication?
ASP.NET MVC 如何使用 Form Authentication?
532 0
|
开发框架 .NET API
一个简单的 ASP.NET Core 依赖注入例子,提高代码的可维护性和可扩展性
一个简单的 ASP.NET Core 依赖注入例子,提高代码的可维护性和可扩展性
279 0
|
开发框架 .NET
Asp.Net Core 使用X.PagedList.Mvc.Core分页 & 搜索
Asp.Net Core 使用X.PagedList.Mvc.Core分页 & 搜索
847 0
|
开发框架 前端开发 .NET
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
916 0
|
存储 开发框架 前端开发
[回馈]ASP.NET Core MVC开发实战之商城系统(五)
经过一段时间的准备,新的一期【ASP.NET Core MVC开发实战之商城系统】已经开始,在之前的文章中,讲解了商城系统的整体功能设计,页面布局设计,环境搭建,系统配置,及首页【商品类型,banner条,友情链接,降价促销,新品爆款】,商品列表页面,商品详情等功能的开发,今天继续讲解购物车功能开发,仅供学习分享使用,如有不足之处,还请指正。
524 0
|
开发框架 前端开发 .NET
[回馈]ASP.NET Core MVC开发实战之商城系统(三)
[回馈]ASP.NET Core MVC开发实战之商城系统(三)
379 0