工作笔记:一个分页类
/* ----------------------------------------------------------
* 分页
* Programming by Ahbool
* Created: 2009-5-10
* Last Updated: 2009-8-4
* E-Mail:liaojunbocn@gmail.com
*
*
*
* 使用方法:
*
* Pagination pg = new Pagination();
*
* //..... 这里可以修改一些属性
*
* DataTable myTable = pg.pagination(tb,20,""); //返回当前页的数据表
*
* string pageInfo = pg.pageNumList; //生成的页码
*
*
---------------------------------------------------------- */
using System;
using System.Web;
using System.Text;
using System.Data;
/// <summary>
/// 使用时请忽略此类 :)
/// </summary>
public class PaginationAttribute
{
protected string _prevURL = null;
protected string _nextURL = null;
protected string _firstURL = null;
protected string _lastURL = null;
protected string _pageNumList = null;
protected int _curPage;
protected int _countPage;
protected int _countRows;
private int _frontBackNum;
private int _jumpNum;
/// <summary>
/// 上一页文本,默认"上一页"
/// </summary>
public string prevText { get; set; }
/// <summary>
/// 下一页文本,默认"下一页"
/// </summary>
public string nextText { get; set; }
/// <summary>
/// 第一页文本,默认"首页"
/// </summary>
public string firstText { get; set; }
/// <summary>
/// 最后一页文本,默认"尾页"
/// </summary>
public string lastText { get; set; }
/// <summary>
/// 向前跳越文本,默认"..."
/// </summary>
public string frontJumpText { get; set; }
/// <summary>
/// 向后跳越文本,默认"..."
/// </summary>
public string backJumpText { get; set; }
/// <summary>
/// 获取最终显示在页面上的页码列表
///
/// 页码HTML结构:
/// 页码包含于" <!--<div class='pagination'>...</div>--> 中
/// 链接包含于 <!-- <a>...</a>--> 中
/// 当前页包含于 <!-- <span>...</span>--> 中
/// </summary>
public string pageNumList
{
get { return _pageNumList; }
}
/// <summary>
/// 获取上一页的URL
/// </summary>
public string prevURL
{
get { return _prevURL; }
}
/// <summary>
/// 获取下一页的URL
/// </summary>
public string nextURL
{
get { return _nextURL; }
}
/// <summary>
/// 获取第一页的URL
/// </summary>
public string firstURL
{
get { return _firstURL; }
}
/// <summary>
/// 获取最后一页的URL
/// </summary>
public string lastURL
{
get { return _lastURL; }
}
/// <summary>
/// 获取或设置显示的页码基数
///
/// 最终显示页码数=(基数*2)+1
/// </summary>
public int frontBackNum
{
get { return _frontBackNum; }
set
{
if (value > 0)
_frontBackNum = value;
else
_frontBackNum = 0;
}
}
/// <summary>
/// 获取或设置当前页的基础上往前(后)跳越的页数
/// </summary>
public int jumpNum
{
get { return _jumpNum; }
set { _jumpNum = value; }
}
}
public class Pagination
: PaginationAttribute
{
public Pagination()
{
prevText = "上一页";
nextText = "下一页";
firstText = "首页";
lastText = "尾页";
frontJumpText = "...";
backJumpText = "...";
frontBackNum = 4;
jumpNum = 10;
}
/// <summary>
/// 一次性查出所有数据,程序控制每页显示的数据
///
/// (适用于缓存分页)
/// </summary>
/// <param name="myTable">进行分页的数据表</param>
/// <param name="showRows">每页显示行数</param>
/// <param name="URLparam">URL参数,可为空</param>
/// <returns>当前页的数据(DataTable)</returns>
public DataTable pagination(DataTable myTable, int showRows, params String[] URLparams)
{
_countRows = myTable.Rows.Count;
if (_countRows % showRows == 0)
{
_countPage = _countRows / showRows;
}
else
{
_countPage = _countRows / showRows + 1;
}
if (HttpContext.Current.Request.QueryString["page"] != null)
{
bool requestPageIsNum = true;
String curPageStr = HttpContext.Current.Request.QueryString["page"];
foreach (Char ch in curPageStr)
{
if (!char.IsNumber(ch))
{
requestPageIsNum = false;
break;
}
}
if (requestPageIsNum)
{
_curPage = Convert.ToInt32(HttpContext.Current.Request.QueryString["page"]);
_curPage = _curPage < 1 ? 1 : _curPage;
}
else
{
_curPage = 1;
}
}
else
{
_curPage = 1;
}
if (_curPage > _countPage) _curPage = _countPage;
int begin = (_curPage - 1) * showRows;
int over = (_curPage * showRows) - 1;
while (myTable.Rows.Count > _countRows - begin)
{
myTable.Rows.RemoveAt(0);
}
while (myTable.Rows.Count > over - begin + 1)
{
myTable.Rows.RemoveAt(over - begin + 1);
}
String paramList = String.Empty;
String paramValue = String.Empty;
String url = HttpContext.Current.Request.CurrentExecutionFilePath;
foreach (String par in URLparams)
{
paramValue = HttpContext.Current.Request.QueryString[par];
if (!String.IsNullOrEmpty(paramValue))
{
paramValue = HttpUtility.UrlEncode(HttpUtility.UrlDecode(paramValue));
paramList += par + "=" + paramValue + "&";
}
}
url += "?" + paramList + "page=";
_firstURL = url + "1";
_lastURL = url + _countPage.ToString();
if (_curPage > 0)
_prevURL = url + Convert.ToString(_curPage - 1);
else
_prevURL = firstURL;
if (_curPage < _countPage)
_nextURL = url + Convert.ToString(_curPage + 1);
else
_nextURL = lastURL;
//-----------页码
StringBuilder sbNumList = new StringBuilder();
sbNumList.Append("<div class='pagination'>");
if (_curPage == 1)
{
sbNumList.AppendFormat("<a>{0}</a>", prevText);
}
else
{
sbNumList.AppendFormat("<a href='{0}'>{1}</a>", _prevURL, prevText);
}
int startNumber = 0;
if ((_curPage - frontBackNum) <= 1)
{
startNumber = 2;
}
else if ((_curPage + frontBackNum) >= (_countPage - 1))
{
startNumber = _countPage - (frontBackNum * 2 + 1);
}
else
{
startNumber = _curPage - frontBackNum;
}
int endNumber = (startNumber + frontBackNum * 2) >= _countPage ? (_countPage - 1) : (startNumber + frontBackNum * 2);
if (_curPage > frontBackNum + 2)
{
int frontJumpNum = (_curPage - jumpNum) <= 1 ? 2 : (_curPage - jumpNum);
sbNumList.AppendFormat("<a href='{0}'>1</a>", firstURL);
sbNumList.AppendFormat("<a href='{0}'>{1}</a>", (url + frontJumpNum), frontJumpText);
}
else if (_curPage == 1)
{
sbNumList.Append("<span>1</span>");
}
else
{
sbNumList.AppendFormat("<a href='{0}'>1</a>", firstURL);
}
while (startNumber <= endNumber)
{
if (startNumber == _curPage)
{
sbNumList.AppendFormat("<span>{0}</span>", startNumber);
}
else
{
sbNumList.AppendFormat("<a href='{0}'>{1}</a>", (url + startNumber), startNumber);
}
startNumber++;
}
if (endNumber < _countPage - 1)
{
int backJumpNum = (_curPage + jumpNum) >= _countPage ? (_countPage - 1) : (_curPage + jumpNum);
sbNumList.AppendFormat("<a href='{0}'>{1}</a>", (url + backJumpNum), backJumpText);
sbNumList.AppendFormat("<a href='{0}'>{1}</a>", lastURL, _countPage);
}
else if (_curPage == _countPage)
{
sbNumList.AppendFormat("<span>{0}</span>", _countPage);
}
else
{
sbNumList.AppendFormat("<a href='{0}'>{1}</a>", lastURL, _countPage);
}
if (_curPage == _countPage)
{
sbNumList.AppendFormat("<a>{0}</a>", nextText);
}
else
{
sbNumList.AppendFormat("<a href='{0}'>{1}</a>", nextURL, nextText);
}
sbNumList.Append("</div>");
//-------------
_pageNumList = sbNumList.ToString();
return myTable;
}
/// <summary>
/// 翻一页,便到数据库读取当前页所需的数据
///
/// (适用于存储过程分页)
/// </summary>
/// <param name="countRows">进行分数据总行数</param>
/// <param name="showRows">每页显示行数</param>
/// <param name="URLparam">URL参数</param>
public void pagination(int countRows, int showRows, params String[] URLparams)
{
_countRows = countRows;
if (_countRows % showRows == 0)
{
_countPage = _countRows / showRows;
}
else
{
_countPage = _countRows / showRows + 1;
}
if (HttpContext.Current.Request.QueryString["page"] != null)
{
bool requestPageIsNum = true;
String curPageStr = HttpContext.Current.Request.QueryString["page"];
foreach (Char ch in curPageStr)
{
if (!char.IsNumber(ch))
{
requestPageIsNum = false;
break;
}
}
if (requestPageIsNum)
{
_curPage = Convert.ToInt32(HttpContext.Current.Request.QueryString["page"]);
_curPage = _curPage < 1 ? 1 : _curPage;
}
else
{
_curPage = 1;
}
}
else
{
_curPage = 1;
}
if (_curPage > _countPage) _curPage = _countPage;
String paramList = String.Empty;
String paramValue = String.Empty;
String url = HttpContext.Current.Request.CurrentExecutionFilePath;
foreach (String par in URLparams)
{
paramValue = HttpContext.Current.Request.QueryString[par];
if (!String.IsNullOrEmpty(paramValue))
{
paramValue = HttpUtility.UrlEncode(HttpUtility.UrlDecode(paramValue));
paramList += par + "=" + paramValue + "&";
}
}
url += "?" + paramList + "page=";
_firstURL = url + "1";
_lastURL = url + _countPage.ToString();
if (_curPage > 0)
_prevURL = url + Convert.ToString(_curPage - 1);
else
_prevURL = firstURL;
if (_curPage < _countPage)
_nextURL = url + Convert.ToString(_curPage + 1);
else
_nextURL = lastURL;
//-----------页码
StringBuilder sbNumList = new StringBuilder();
sbNumList.Append("<div class='pagination'>");
if (_curPage == 1)
{
sbNumList.AppendFormat("<a>{0}</a>", prevText);
}
else
{
sbNumList.AppendFormat("<a href='{0}'>{1}</a>", _prevURL, prevText);
}
int startNumber = 0;
if ((_curPage - frontBackNum) <= 1)
{
startNumber = 2;
}
else if ((_curPage + frontBackNum) >= (_countPage - 1))
{
startNumber = _countPage - (frontBackNum * 2 + 1);
}
else
{
startNumber = _curPage - frontBackNum;
}
int endNumber = (startNumber + frontBackNum * 2) >= _countPage ? (_countPage - 1) : (startNumber + frontBackNum * 2);
if (_curPage > frontBackNum + 2)
{
int frontJumpNum = (_curPage - jumpNum) <= 1 ? 2 : (_curPage - jumpNum);
sbNumList.AppendFormat("<a href='{0}'>1</a>", firstURL);
sbNumList.AppendFormat("<a href='{0}'>{1}</a>", (url + frontJumpNum), frontJumpText);
}
else if (_curPage == 1)
{
sbNumList.Append("<span>1</span>");
}
else
{
sbNumList.AppendFormat("<a href='{0}'>1</a>", firstURL);
}
while (startNumber <= endNumber)
{
if (startNumber == _curPage)
{
sbNumList.AppendFormat("<span>{0}</span>", startNumber);
}
else
{
sbNumList.AppendFormat("<a href='{0}'>{1}</a>", (url + startNumber), startNumber);
}
startNumber++;
}
if (endNumber < _countPage - 1)
{
int backJumpNum = (_curPage + jumpNum) >= _countPage ? (_countPage - 1) : (_curPage + jumpNum);
sbNumList.AppendFormat("<a href='{0}'>{1}</a>", (url + backJumpNum), backJumpText);
sbNumList.AppendFormat("<a href='{0}'>{1}</a>", lastURL, _countPage);
}
else if (_curPage == _countPage)
{
sbNumList.AppendFormat("<span>{0}</span>", _countPage);
}
else
{
sbNumList.AppendFormat("<a href='{0}'>{1}</a>", lastURL, _countPage);
}
if (_curPage == _countPage)
{
sbNumList.AppendFormat("<a>{0}</a>", nextText);
}
else
{
sbNumList.AppendFormat("<a href='{0}'>{1}</a>", nextURL, nextText);
}
sbNumList.Append("</div>");
//-------------
_pageNumList = sbNumList.ToString();
}
}
- 评论
-
- [使用Ctrl+Enter键可以直接提交]
表情图标
Advertise
Category
Time Counter
离十一还有
Recent Article
- 1.工作笔记:AS3加载外部图片,有加载百分比
- 2.document.documentElement和document.body的区别
- 3.100种增加网站流量的方法
- 4.用div+css模拟表格对角线
- 5.如何做一个好的技术型领导
- 6.苍井空是谁?
- 7."心态"新解
- 8.程序员特有的9个坏习惯
- 9.AS3入门之简单Loading效果
- 10.系统问题:浏览器无法打开png图片
- 11.AS3中以post和get方式提交数据
- 12.defaultTextFormat和setTextFormat()区别
- 13.AS3中超方便地遍历xml
- 14.pv3d中物体常用的移动属性
- 15.开源Flash 3D引擎Papervision3d
- 16.as3 判断鼠标滚轮前滚或后滚
- 17.js:行向上替换滚动效果
- 18.为什么要清净?(禅与佛)
- 19.扩展window.setTimeout方法
- 20.收集2010的搞笑短句



