好用的ASP.NET 分页类 简单好用 支持 AJAX 自定义文字
在做网站没用 JS UI控件时 很实用
用法:
var ps=new PageString();
/*可选参数*/
ps.SetIsEnglish = true;// 是否是英文 (默认:false)
ps.SetIsShowText = true;//是否显示分页文字 (默认:true)
//ps.TextFormat="" (默认值:《span class=\"pagetext\"》《strong》总共《/strong》:{0} 条 《strong》当前《/strong》:{1}/{2}《/span》)
//ps.SetPageIndexName Request["pageIndex"](默认值:"pageIndex")
ps.SetIsAjax = false;// (默认值:"false")
/*函数参数*/
int total = 10000;
int pageSize = 10;
int pageIndex = Convert.ToInt32(Request["pageIndex"]);
var page = ps.ToString(total, pageSize, pageIndex, "/UI/PageStringTest.aspx?");
//获取 page html 输出
Response.Write(page);
效果:

代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions; namespace SyntacticSugar
{
/// <summary>
/// ** 描述:分页类
/// ** 创始时间:2015-5-29
/// ** 修改时间:-
/// ** 作者:sunkaixuan
public class PageString
{
/// <summary>
/// 是否是英文 (默认:false)
/// </summary>
public bool SetIsEnglish { get; set; }
/// <summary>
/// 是否显示分页文字(默认:true)
/// </summary>
public bool SetIsShowText { get; set; }
/// <summary>
/// 样式 (默认:"pagination")
/// </summary>
public string SetClassName { get; set; }
/// <summary>
/// 分页参数名 (默认:"pageIndex")
/// </summary>
public string SetPageIndexName { get; set; }
/// <summary>
/// 是否是异步 同步 href='' 异步 onclick=ajaxPage() (默认:false)
/// </summary>
public bool SetIsAjax { get; set; } /// <summary>
/// 自定义文字
/// string.Format("{0}{1}{2}","总记录数","当前页数","总页数")
/// 默认值:《span class=\"pagetext\"》《strong》总共《/strong》:{0} 条 《strong》当前《/strong》:{1}/{2}《/span》
/// </summary>
public string SetTextFormat { get; set; } public PageString()
{
SetIsEnglish = false;
SetIsShowText = true;
SetTextFormat = "<span class=\"pagetext\"><strong>总共</strong>:{0} 条 <strong>当前</strong>:{1}/{2}</span> ";
SetClassName = "pagination";
SetPageIndexName = "pageIndex";
SetIsAjax = false;
} /*免费的样式
.pagination .click {cursor:pointer}
.pagination a{text-decoration: none;border: 1px solid #AAE;color: #15B;font-size: 13px;border-radius: 2px;}
.pagination span{ color:#666;font-size:13px;display: inline-block;border: 1px solid #ccc;padding: 0.2em 0.6em;}
.pagination span.pagetext{ border:none}
.pagination a:hover{background: #26B;color: #fff;}
.pagination a{display: inline-block;padding: 0.2em 0.6em;}
.pagination .page_current{background: #26B;color: #fff;border: 1px solid #AAE;margin-right: 5px;}
.pagination{margin-top: 20px;}
.pagination .current.prev, .pagination .current.next{color: #999;border-color: #999;background: #fff;}
* */ /// <summary>
/// 分页算法<一>共20页 首页 上一页 1 2 3 4 5 6 7 8 9 10 下一页 末页
/// </summary>
/// <param name="total">总记录数</param>
/// <param name="pageSize">每页记录数</param>
/// <param name="pageIndex">当前页数</param>
/// <param name="query_string">Url参数</param>
/// <returns></returns>
public string ToString(int total, int pageSize, int pageIndex, string query_string)
{ int allpage = 0;
int next = 0;
int pre = 0;
int startcount = 0;
int endcount = 0;
StringBuilder pagestr = new StringBuilder();
pageIndex = pageIndex == 0 ? 1 : pageIndex;
pagestr.AppendFormat("<div class=\"{0}\" >", SetClassName);
if (pageIndex < 1) { pageIndex = 1; }
//计算总页数
if (pageSize != 0)
{
allpage = (total / pageSize);
allpage = ((total % pageSize) != 0 ? allpage + 1 : allpage);
allpage = (allpage == 0 ? 1 : allpage);
}
next = pageIndex + 1;
pre = pageIndex - 1;
startcount = (pageIndex + 5) > allpage ? allpage - 9 : pageIndex - 4;//中间页起始序号
//中间页终止序号
endcount = pageIndex < 5 ? 10 : pageIndex + 5;
if (startcount < 1) { startcount = 1; } //为了避免输出的时候产生负数,设置如果小于1就从序号1开始
if (allpage < endcount) { endcount = allpage; }//页码+5的可能性就会产生最终输出序号大于总页码,那么就要将其控制在页码数之内 bool isFirst = pageIndex == 1;
bool isLast = pageIndex == allpage; if (SetIsShowText)
pagestr.AppendFormat(SetTextFormat, total, pageIndex, allpage); if (isFirst)
{
pagestr.Append("<span>首页</span> <span>上一页</span>");
}
else
{
pagestr.AppendFormat("<a href=\"{0}pageIndex=1\">首页</a> <a href=\"{0}pageIndex={1}\">上一页</a>", query_string, pre);
}
//中间页处理,这个增加时间复杂度,减小空间复杂度
for (int i = startcount; i <= endcount; i++)
{
bool isCurent = pageIndex == i;
if (isCurent)
{
pagestr.Append(" <a class=\"page_current\">" + i + "</a>");
}
else
{
pagestr.Append(" <a href=\"" + query_string + "pageIndex=" + i + "\">" + i + "</a>");
} }
if (isLast)
{
pagestr.Append("<span>下一页</span> <span>末页</span>");
}
else
{
pagestr.Append(" <a href=\"" + query_string + "pageIndex=" + next + "\">下一页</a> <a href=\"" + query_string + "pageIndex=" + allpage + "\">末页</a>");
}
pagestr.AppendFormat("</div>");
return ConversionData(pagestr.ToString());
} private string ConversionData(string page)
{
if (SetIsEnglish)
{
page= page.Replace("上一页", "Previous").Replace("下一页", "Next").Replace("总共", "total").Replace("当前", "Current").Replace("条", "records").Replace("首页", "First").Replace("末页", "Last");
}
if (SetIsAjax)
{
var matches = Regex.Matches(page, @"href\="".*?""",RegexOptions.Singleline);
if (matches != null && matches.Count > 0)
{
foreach (Match m in matches)
{
page = page.Replace(m.Value, string.Format("class=\"click\" onclick=\"ajaxPage('{0}')\"", Regex.Match(m.Value, string.Format(@"{0}\=(\d+)", SetPageIndexName)).Groups[1].Value));
}
}
}
return page; } } }
好用的ASP.NET 分页类 简单好用 支持 AJAX 自定义文字的更多相关文章
- asp.net 分页类
PaginatedList.cs using System;using System.Collections.Generic;using System.Linq;using System.Web; n ...
- php分页类代码带分页样式效果(转)
php分页类代码,有漂亮的分页样式风格 时间:2016-03-16 09:16:03来源:网络 导读:不错的php分页类代码,将类文件与分页样式嵌入,实现php查询结果的精美分页,对研究php分页原理 ...
- PHP+jQuery 列表分页类 ( 支持 url 分页 / ajax 分页 )
/* ******* 环境:Apache2.2.8 ( 2.2.17 ) + PHP5.2.6 ( 5.3.3 ) + MySQL5.0.51b ( 5.5.8 ) + jQuery-1.8.3.mi ...
- PHP+jQuery 长文章分页类 ( 支持 url / ajax 分页方式 )
/* ******* 环境:Apache2.2.8 ( 2.2.17 ) + PHP5.2.6 ( 5.3.3 ) + MySQL5.0.51b ( 5.5.8 ) + jQuery-1.8 **** ...
- Django 自定义分页类
分页类代码: class Page(object): ''' 自定义分页类 可以实现Django ORM数据的的分页展示 输出HTML代码: 使用说明: from utils import mypag ...
- 【paging_Class 分页类】使用说明
类名:paging_Class 说明:分页类 注意: 1) 支持百万级数据分页 2) 支持多种类型的SQL语法,比如 Left Join 等. 3) 自动保存查询中的错误情况,记录保存在:/Cache ...
- asp.net的快捷实用分页类
KeleyiPager分页类,可以于对列表页进行分页浏览,代码是从HoverTreeCMS项目中COPY的,感觉很不错,使用简单方便,但是功能强大. 在线体验效果:http://cms.hovertr ...
- 自己写的一个ASP.NET服务器控件Repeater和GridView分页类
不墨迹,直接上代码 using System; using System.Collections.Generic; using System.Linq; using System.Text; usin ...
- 简单实用的原生PHP分页类
一款简单实用的原生PHP分页类,分页按钮样式简洁美观,页码多的时候显示“...”,也是挺多网站用的效果 核心分页代码 include_once("config.php"); req ...
随机推荐
- wmi详解,RPC和防火墙
135端口:Microsoft在这个端口运行DCE RPC end-point mapper为它的DCOM服务.这与UNIX 111端口的功能很相似.使用DCOM和RPC的服务利用计算机上的end-p ...
- 解放双手——Android自动化测试
解放程序猿宝贵的右手(或者是左手) http://blog.csdn.net/eclipsexys/article/details/45622813 --Android自动化测试技巧 Google大神 ...
- Conway's Game of Life: An Exercise in WPF, MVVM and C#
This blog post was written for the Lockheed Martin Insight blog, sharing here for the external audie ...
- [转载] Calculating Entropy
From: johndcook.com/blog For a set of positive probabilities pi summing to 1, their entropy is defi ...
- checkbox与说明文字无法对齐的问题
解决方法: vertical-align:middle; 例:<input type=checkbox id="theId" name=checkbox style=&quo ...
- 解决yum错误:Cannot retrieve repository metadata (repomd.xml) for repository
解决方法如下: # cd /etc/ #ls 找到yum.repos.d这个目录,里面有个文件CentOS-Media.repo(你的机器里也许不是这个名字,名称应该是自定义的),vi 编译一下里面的 ...
- Android Studio 经常使用功能介绍
为了简化 Android 的开发力度,Google 决定将重点建设 Android Studio 工具.Google 会在今年年底停止支持其它集成开发环境.比方 Eclipse. Android St ...
- PHP 中 Orientation 属性判断上传图片是否需要旋转(转)
<?php $image = imagecreatefromstring(file_get_contents($_FILES['image_upload']['tmp_name'])); $ex ...
- 针对 SQL Server 2008 在Windows Server 2008上的访问配置 Windows 防火墙
现在Windows Server 2008 服务器用的越来越多,2008的防火墙比2003的有了很大的增强,安全性有了更大的提高. 甚至80端口的出站默认都是被关闭的.所以如果在2008Server上 ...
- 查看kernel log命令
adb shell "cat /dev/kmsg | grep -Ei "gesture""