一个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 >

 

目录
相关文章
|
2月前
|
开发框架 JavaScript .NET
asp.net中条件查询+分页
asp.net中条件查询+分页
17 1
|
4月前
|
开发框架 前端开发 .NET
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
46 0
|
16天前
|
开发框架 前端开发 JavaScript
JavaScript云LIS系统源码ASP.NET CORE 3.1 MVC + SQLserver + Redis医院实验室信息系统源码 医院云LIS系统源码
实验室信息系统(Laboratory Information System,缩写LIS)是一类用来处理实验室过程信息的软件,云LIS系统围绕临床,云LIS系统将与云HIS系统建立起高度的业务整合,以体现“以病人为中心”的设计理念,优化就诊流程,方便患者就医。
22 0
|
2月前
|
开发框架 前端开发 .NET
进入ASP .net mvc的世界
进入ASP .net mvc的世界
32 0
|
2月前
|
开发框架 前端开发 .NET
C# .NET面试系列六:ASP.NET MVC
<h2>ASP.NET MVC #### 1. MVC 中的 TempData\ViewBag\ViewData 区别? 在ASP.NET MVC中,TempData、ViewBag 和 ViewData 都是用于在控制器和视图之间传递数据的机制,但它们有一些区别。 <b>TempData:</b> 1、生命周期 ```c# TempData 的生命周期是短暂的,数据只在当前请求和下一次请求之间有效。一旦数据被读取,它就会被标记为已读,下一次请求时就会被清除。 ``` 2、用途 ```c# 主要用于在两个动作之间传递数据,例如在一个动作中设置 TempData,然后在重定向到另
110 5
|
5月前
|
前端开发 JavaScript Java
让你了解什么是spring MVC模型数据(附大量代码)
让你了解什么是spring MVC模型数据(附大量代码)
45 0
|
6月前
|
开发框架 自然语言处理 前端开发
基于ASP.NET MVC开发的、开源的个人博客系统
基于ASP.NET MVC开发的、开源的个人博客系统
52 0
|
6月前
|
开发框架 前端开发 JavaScript
Asp.net动态加载用户自定义控件,并转换成HTML代码
Asp.net动态加载用户自定义控件,并转换成HTML代码
27 0
|
9月前
|
SQL 开发框架 前端开发
[回馈]ASP.NET Core MVC开发实战之商城系统(完:内附源码)
经过一段时间的准备,【ASP.NET Core MVC开发实战之商城系统】已经完成,目前代码已开发完成,先将全部内容整理分享,如有不足之处,还请指正。
111 0
|
9月前
|
存储 开发框架 前端开发
[回馈]ASP.NET Core MVC开发实战之商城系统(五)
经过一段时间的准备,新的一期【ASP.NET Core MVC开发实战之商城系统】已经开始,在之前的文章中,讲解了商城系统的整体功能设计,页面布局设计,环境搭建,系统配置,及首页【商品类型,banner条,友情链接,降价促销,新品爆款】,商品列表页面,商品详情等功能的开发,今天继续讲解购物车功能开发,仅供学习分享使用,如有不足之处,还请指正。
124 0