CODE:
using System;
using System.Collections.Generic;
using System.Text;
/**//// <summary>
/// .Net通用分页类(存储过程分页版,可以选择页码的显示样式,且有中英选择)
/// 作者:启程 www.letwego.cn
/// 可用于任意用途,请保留作者信息,谢谢!
/// </summary>
namespace letwego.cn
{
public class PageStore
{
初始构造#region 初始构造
//PageStore()
//{
//}
#endregion
字段#region 字段
/**//// <summary>
/// 每页记录数
/// </summary>
private int _PageSize = 20;
/**//// <summary>
/// 需要获取第几页的数据,从 1 开始
/// </summary>
private int _PageIndex = 1;
/**//// <summary>
/// 总页数
/// </summary>
private int _PageCounts = 0;
/**//// <summary>
/// 总记录数
/// </summary>
private int _Counts = 0;
/**//// <summary>
/// 首页 显示样式
/// </summary>
private string _FirstStr = "";
/**//// <summary>
/// 上一页 显示样式
/// </summary>
private string _PrevStr = "";
/**//// <summary>
/// 下一页 显示样式
/// </summary>
private string _NextStr = "";
/**//// <summary>
/// 尾页 显示样式
/// </summary>
private string _LastStr = "";
/**//// <summary>
/// 跳转 的url链接
/// </summary>
private string _TurnUrlStr = "";
/**//// <summary>
/// 跳转的url链接的参数前面不要加问号和与号
/// </summary>
private string _Options = "";
private string strCountww = ""; //共N条信息
private string strPageww = ""; //第N页/共N页
private string strTurnww; //跳转控件
#endregion
属性#region 属性
/**//// <summary>
/// 每页记录数
/// </summary>
public int PageSize
{
get { return _PageSize; }
set { _PageSize = value; }
}
/**//// <summary>
/// 需要获取第几页的数据,从 1 开始
/// </summary>
public int PageIndex
{
get { return _PageIndex; }
set { _PageIndex = value; }
}
/**//// <summary>
/// 总页数
/// </summary>
public int PageCounts
{
get { return _PageCounts; }
}
/**//// <summary>
/// 总记录数
/// </summary>
public int Counts
{
get { return _Counts; }
set { _Counts = value; }
}
/**//// <summary>
/// 首页 显示样式
/// </summary>
public string FirstStr
{
get { return _FirstStr; }
set { _FirstStr = value; }
}
/**//// <summary>
/// 上一页 显示样式
/// </summary>
public string PrevStr
{
get { return _PrevStr; }
set { _PrevStr = value; }
}
/**//// <summary>
/// 下一页 显示样式
/// </summary>
public string NextStr
{
get { return _NextStr; }
set { _NextStr = value; }
}
/**//// <summary>
/// 尾页 显示样式
/// </summary>
public string LastStr
{
get { return _LastStr; }
set { _LastStr = value; }
}
/**//// <summary>
/// 跳转 的url链接
/// </summary>
public string TurnUrlStr
{
get { return _TurnUrlStr; }
set { _TurnUrlStr = value; }
}
/**//// <summary>
/// 跳转的url链接的参数前面不要加问号和与号
/// </summary>
public string Options
{
get { return _Options; }
set { _Options = value; }
}
#endregion
返回分页后的页码显示#region 返回分页后的页码显示
/**//// <summary>
/// 返回分页后的页码显示
/// </summary>
/// <param name="bolCount">是否显示 共N条信息</param>
/// <param name="bolPage">是否显示 第N页/共N页</param>
/// <param name="bolFirst">是否显示 首页</param>
/// <param name="bolLast">是否显示 尾页</param>
/// <param name="bolTurn">是否显示 跳转控件</param>
/// <param name="IsChinese">是否 用中文显示</param>
/// <param name="intStyle">样式选择</param>
/// <param name="intShowNum">每页显示多少个数字</param>
/// <returns>返回分页后的页码显示</returns>
public string GetShowPageStr(bool bolCount, bool bolPage, bool bolFirst, bool bolLast, bool bolTurn, bool IsChinese, int intStyle, int intShowNum)
{
string strPageShowww = "";
string _FirstStr2 = "";
string _PrevStr2 = "";
string _NextStr2 = "";
string _LastStr2 = "";
公共处理#region 公共处理
//总页数
_PageCounts = (_Counts + _PageSize - 1) / _PageSize;
//超出最小页码
if (_PageIndex < 1)
{
_PageIndex = 1;
}
//超出最大页码
if (_PageIndex > _PageCounts)
{
_PageIndex = _PageCounts;
}
if (IsChinese)//中文分页
{
//跳转
strTurnww = "<input value='" + _PageIndex.ToString() + "' id='txtPageGo' name='txtPageGo' type='text' style='width:35px;'><input name='btnGo' type='button' id='btnGo' value='跳转' onclick=""javascript:window.location.href='" + _TurnUrlStr + "?Page=' + document.getElementById('txtPageGo').value + '" + "&" + Options + "'"">";
//共N条信息
strCountww = "共 " + _Counts.ToString() + " 条信息";
//第N页/共N页
strPageww = "第" + _PageIndex.ToString() + "页/共" + _PageCounts.ToString() + "页";
//处理页码显示样式
if (intStyle == 1)
{
if (_FirstStr == "")
{
_FirstStr = "首页";
}
if (_PrevStr == "")
{
_PrevStr = "上一页";
}
if (_NextStr == "")
{
_NextStr = "下一页";
}
if (_LastStr == "")
{
_LastStr = "尾页";
}
}
else
{
if (_FirstStr == "")
{
_FirstStr = " << ";
}
if (_PrevStr == "")
{
_PrevStr = " < ";
}
if (_NextStr == "")
{
_NextStr = " > ";
}
if (_LastStr == "")
{
_LastStr = " >> ";
}
}
}
else//英文文分页
{
//跳转
strTurnww = "<input value='" + _PageIndex.ToString() + "' id='txtPageGo' name='txtPageGo' type='text' style='width:35px;'><input name='btnGo' type='button' id='btnGo' value='Goto' onclick=""javascript:window.location.href='" + _TurnUrlStr + "?Page=' + document.getElementById('txtPageGo').value + '" + "&" + Options + "'"">";
//共N条信息
strCountww = "Total " + _Counts.ToString() + " Infos";
//第N页/共N页
strPageww = " " + _PageIndex.ToString() + "/" + _PageCounts.ToString() + " ";
//处理页码显示样式
if (intStyle == 1)
{
if (_FirstStr == "")
{
_FirstStr = " First ";
}
if (_PrevStr == "")
{
_PrevStr = " Previous ";
}
if (_NextStr == "")
{
_NextStr = " Next ";
}
if (_LastStr == "")
{
_LastStr = " Last ";
}
}
else
{
if (_FirstStr == "")
{
_FirstStr = " << ";
}
if (_PrevStr == "")
{
_PrevStr = " < ";
}
if (_NextStr == "")
{
_NextStr = " > ";
}
if (_LastStr == "")
{
_LastStr = " >> ";
}
}
}
#endregion
//没有记录
if (_Counts <= 0)
{
strPageShowww = strCountww;
}
//有记录
else
{
//只有一页
if (_PageCounts <= 1)
{
strPageShowww = strCountww + " " + strPageww;
}
//不止一页
else
{
页码链接处理#region 页码链接处理
//第一页
if (_PageIndex == 1)
{
_FirstStr2 = _FirstStr;
_PrevStr2 = _PrevStr;
}
else
{
_FirstStr2 = "<a href=""" + _TurnUrlStr + "?Page=1" + "&" + _Options + """>" + _FirstStr + "</a>";
_PrevStr2 = "<a href=""" + _TurnUrlStr + "?Page=" + Convert.ToString(_PageIndex - 1) + "&" + _Options + """>" + _PrevStr + "</a>";
}
//最后一页
if (_PageIndex == _PageCounts)
{
_NextStr2 = _NextStr;
_LastStr2 = _LastStr;
}
else
{
_NextStr2 = "<a href=""" + _TurnUrlStr + "?Page=" + Convert.ToString(_PageIndex + 1) + "&" + _Options + """>" + _NextStr + "</a>";
_LastStr2 = "<a href=""" + _TurnUrlStr + "?Page=" + _PageCounts + "&" + _Options + """>" + _LastStr + "</a>";
}
//----处理显示页码-----------
if (bolCount == true)//共N条信息
{
strPageShowww = strPageShowww + " " + strCountww;
}
if (bolPage == true)//第N页/共N页
{
strPageShowww = strPageShowww + " " + strPageww;
}
if (bolFirst == true) //首页
{
strPageShowww = strPageShowww + " " + _FirstStr2;
}
strPageShowww = strPageShowww + "{0}";//上一页
strPageShowww = strPageShowww + "{1}{2}";//下一页
if (bolLast == true)//尾页
{
strPageShowww = strPageShowww + " " + _LastStr2;
}
if (bolTurn == true)//跳转控件
{
strPageShowww = strPageShowww + " " + strTurnww;
}
#endregion
样式一: 共X条信息 第N页/共M页 首页 上一页 下一页 尾页 跳转#region 样式一: 共X条信息 第N页/共M页 首页 上一页 下一页 尾页 跳转
if (intStyle == 1)
{
strPageShowww = strPageShowww.Replace("{0}", " " + _PrevStr2);//上一页
strPageShowww = strPageShowww.Replace("{1}", " " + _NextStr2);//下一页
strPageShowww = strPageShowww.Replace("{2}", "");//
}
#endregion
样式二: 共X条信息 第N页/共M页 首页 1 2 3 尾页 跳转#region 样式二: 共X条信息 第N页/共M页 首页 1 2 3 尾页 跳转
if (intStyle == 2)
{
int PageTemp = 0;
string strPageNum = "";
string strTempNow = "";
//当页码超过最后一批该显示
if (_PageIndex > _PageCounts - intShowNum + 1)
{
PageTemp = _PageCounts < intShowNum ? 0 : _PageCounts - intShowNum;
for (int i = 1; i <= intShowNum; i++)
{
if (i > _PageCounts) break;
strTempNow = Convert.ToString(PageTemp + i);
//当前页不显示超链接
if( PageIndex == PageTemp + i)
{
strPageNum = strPageNum + "<b>" + strTempNow + "</b> ";
}
else
{
strPageNum = strPageNum + "<a href=""" + _TurnUrlStr + "?Page=" + strTempNow + "&" + _Options + """>" + strTempNow + "</a> ";
}
}
}
else
{
for (int i = 0; i < intShowNum; i++)
{
strTempNow = Convert.ToString(PageIndex + i);
//当前页不显示超链接
if (i == 0)
{
strPageNum = strPageNum + "<b>" + strTempNow + "</b> ";
}
else
{
strPageNum = strPageNum + "<a href=""" + _TurnUrlStr + "?Page=" + strTempNow + "&" + _Options + """>" + strTempNow + "</a> ";
}
}
}
//
strPageShowww = strPageShowww.Replace("{0}", " " + _PrevStr2);//上一页
strPageShowww = strPageShowww.Replace("{1}", " " + strPageNum);//显示数字
strPageShowww = strPageShowww.Replace("{2}", " " + _NextStr2);//下一页
}
#endregion
}
}
return strPageShowww;
}
#endregion
}
调用实例#region 调用实例
/**//*
protected void BindData()
{
int MyPage;
string strPage = Request["page"];
if (strPage == null || strPage == "")
{
MyPage = 1;
}
else
{
MyPage = Convert.ToInt32(strPage);
}
PageStore MyPageStore = new PageStore();
MyPageStore.Counts = 100;//总记录数
MyPageStore.PageIndex = MyPage;//当前要显示的页码
MyPageStore.PageSize = 2;//每页显示记录数
MyPageStore.TurnUrlStr = "WebPageStore.aspx";//要跳转的页面(当前页)
MyPageStore.Options = "Options=List";//页面所带参数
this.Label1.Text = MyPageStore.GetShowPageStr(true, true, true, true, true, true, 2, 4);
}
*/
#endregion
简要说明#region 简要说明
/**//*
.Net通用分页类(存储过程分页版,可以选择页码的显示样式,且有中英选择).
大概思路是:主要是利用存储过程在数据库进行分页,
所以在这个类里面不涉及到数据的处理,只进行页码的显示格式处理,
配合SQL2005 的 ROW_NUMBER () 功能,能够达到更好的效果.
目前一共4种样式,4种样式里还可以设置参数,把页码显示调整到最简,只剩下 上一页 下一页,
且 上一页 下一页 等文字可以自定义,用图片也可以,只是要把字符串拼成图片的Html
效果图:https://images.cnblogs.com/cnblogs_com/84ww/128905/r_PageStore.gif
*/
#endregion
附 存储过程例子#region 附 存储过程例子
/**//*
CREATE PROCEDURE dbo.sp_userinfoList
@strOptions varchar(200) = NULL,
@PageSize int = 20,
@PageIndex int = 1,
@Counts int = 0 OUTPUT
AS
SET NOCOUNT ON
SET ANSI_WARNINGS OFF
DECLARE @PageUp int
DECLARE @PageDown int
--处理页大小
IF @PageSize = -1 SET @PageSize = 20
--获得总记录数
SELECT @Counts = COUNT(1) FROM userinfo
--当前页的第一条记录RowID
SET @PageDown = @PageSize * @PageIndex + 1
--当前页的最后一条记录RowID
SET @PageUp = @PageSize * (@PageIndex + 1)
-- 信息列表
IF @strOptions='LIST' BEGIN
SELECT
id,
user,
password,
datetime,
content
FROM
(
SELECT * ,
PageTableRowID = ROW_NUMBER () OVER (ORDER BY id DESC)
FROM userinfo
) AS PageTableList
WHERE PageTableRowID BETWEEN @PageDown AND @PageUp
END
*/
#endregion
}