public class PagerHelper
{
private int _pageSize = 10;
///
/// 每页默认10条数
///
public int pageSize
{
get
{
if (_pageSize < 1)
_pageSize = 1;
return _pageSize;
}
set { _pageSize = value; }
}
private int _extendPage = 5;
///
/// 每页显示分页数
///
public int extendPage
{
get
{
if (_extendPage < 3)
_extendPage = 3;
return _extendPage;
}
set
{
_extendPage = value;
}
}
private int _currentPage = 1;
///
/// 当前页码
///
public int currentPage
{
get
{
if (_currentPage < 1)
_currentPage = 1;
if (_currentPage > totalPages)
_currentPage = totalPages;
return _currentPage;
}
set
{
_currentPage = value;
}
}
private int _totalRows = 0;
///
/// 总数据数
///
public int totalRows
{
get { return _totalRows; }
set { _totalRows = value; }
}
///
/// 分页时是否做urlRewriter处理
///
private bool isRewrite
{
get { return pageUrl.IndexOf("{0}") > 0; }
}
private string _pageUrl = "";
///
/// 分页url
///
public string pageUrl
{
get { return _pageUrl; }
set { _pageUrl = value; }
}
///
/// 总页数
///
private int totalPages
{
get
{
if ((totalRows % pageSize) == 0)
{
return (totalRows / pageSize);
}
return (totalRows / pageSize + 1);
}
}
///
/// 上一页
///
private int prevPage
{
get
{
int temp = currentPage - 1;
if (temp < 1)
{
return 1;
}
return temp;
}
}
///
/// 下一页
///
private int nextPage
{
get
{
int temp = currentPage + 1;
if (temp > totalPages)
{
return temp;
}
return temp;
}
}
///
/// 获得开始显示的页码
///
private int startPage
{
get
{
int _startPage = 0;
if (totalPages > extendPage)
{
if (currentPage - (extendPage / 2) > 0)
{
if (currentPage + (extendPage / 2) < totalPages)
{
_startPage = currentPage - (extendPage / 2);
}
else
{
_startPage = endPage - extendPage + 1;
}
}
else
{
_startPage = 1;
}
}
else
{
_startPage = 1;
}
return _startPage;
}
}
///
/// 获取最后一个显示的页面
///
private int endPage
{
get
{
int _endPage = 0;
if (totalPages > extendPage)
{
if (currentPage - (extendPage / 2) > 0)
{
if (currentPage + (extendPage / 2) < totalPages)
{
_endPage = startPage + extendPage - 1;
}
else
{
_endPage = totalPages;
}
}
else
{
_endPage = extendPage;
}
}
else
{
_endPage = totalPages;
}
return _endPage;
}
}
private string _HtmlDisablePrevPage = "";
///
/// 第一页时上一页的样式
///
public string HtmlDisablePrevPage
{
get { return _HtmlDisablePrevPage; }
set { _HtmlDisablePrevPage = value; }
}
private string _HtmlDisableNextPage = "";
///
/// 最后页时下一页的样式
///
public string HtmlDisableNextPage
{
get { return _HtmlDisableNextPage; }
set { _HtmlDisableNextPage = value; }
}
private string _HtmlCurOfTotalPage = "";
///
/// 当前第几页 例如:第 2/5 页
///
public string HtmlCurOfTotalPage
{
get { return _HtmlCurOfTotalPage; }
set { _HtmlCurOfTotalPage = value; }
}
private bool _bHtmlCurOfTotalPage = true;
///
/// 当前第几页 例如:第 2/5 页
///
public bool bHtmlCurOfTotalPage
{
get { return _bHtmlCurOfTotalPage; }
set { _bHtmlCurOfTotalPage = value; }
}
private string _HtmlTotalCount = "";
///
/// 总记录数的显示形式
///
public string HtmlTotalCount
{
get { return _HtmlTotalCount; }
set { _HtmlTotalCount = value; }
}
private string _HTMLMoveToPage = "";
///
/// 跳转到的格式HTML
///
public string HTMLMoveToPage
{
get
{
string strReturnHtml = string.Format(_HTMLMoveToPage, currentPage);
if (isRewrite)
{
strReturnHtml += "";
}
else
{
if (pageUrl.IndexOf("?") > 0)
strReturnHtml += "";
else
strReturnHtml += "";
}
return strReturnHtml;
}
set
{
_HTMLMoveToPage = value;
}
}
private string _HtmlPrevPage = @"";
///
/// 上一页代码
///
public string HtmlPrevPage
{
get
{
if (isRewrite)
{
if (currentPage < 2 )
{
return _HtmlDisablePrevPage;
}
else
{
return string.Format(_HtmlPrevPage, string.Format(pageUrl, 1), string.Format(pageUrl, prevPage));
}
}
else
{
if (currentPage < 2)
{
return _HtmlDisablePrevPage;
}
else
{
if (pageUrl.IndexOf("?") > 0)
return string.Format(_HtmlPrevPage, pageUrl + "&pn=1", pageUrl + string.Format("&pn={0}", prevPage));
else
return string.Format(_HtmlPrevPage, pageUrl + "?pn=1", pageUrl + string.Format("?pn={0}", prevPage));
}
}
}
set
{
_HtmlPrevPage = value;
}
}
private string _HtmlNextPage = @"";
///
/// 下一页html代码
///
public string HtmlNextPage
{
get
{
if (isRewrite)
{
if (currentPage < totalPages)
{
return string.Format(_HtmlNextPage, string.Format(pageUrl, nextPage), string.Format(pageUrl, totalPages));
}
else
{
return _HtmlDisableNextPage;
}
}
else
{
if (currentPage < totalPages)
{
if (pageUrl.IndexOf("?") > 0)
return string.Format(_HtmlNextPage, pageUrl + string.Format("&pn={0}", nextPage), pageUrl + string.Format("&pn={0}", totalPages));
else
return string.Format(_HtmlNextPage, pageUrl + string.Format("?pn={0}", nextPage), pageUrl + string.Format("?pn={0}", totalPages));
}
else
{
return _HtmlDisableNextPage;
}
}
}
set
{
_HtmlNextPage = value;
}
}
private string _HtmlCurrentPage = @"{0} ";
///
/// 当前页html代码
///
public string HtmlCurrentPage
{
get { return _HtmlCurrentPage; }
set { _HtmlCurrentPage = value; }
}
private string _HtmlPageNum = "";
///
/// 页码数的html
///
public string HtmlPageNum
{
get { return _HtmlPageNum; }
set { _HtmlPageNum = value; }
}
private string _HtmlPageTag = @" ";
///
/// 用于设置分页html外面的div, span等标
///
public string HtmlPageTag
{
get { return _HtmlPageTag; }
set { _HtmlPageTag = value; }
}
///
/// 获取最终html
///
///
public string GetHtmlPage()
{
return GetHtmlPage(false);
}
///
/// 获取最终html
///
///
public string GetHtmlPage(bool bGoToPage)
{
if (totalPages <= 0)
return "";
StringBuilder s = new StringBuilder("");
s.Append(HtmlPrevPage);
for (int i = startPage; i <= endPage; i++)
{
if (i == currentPage)
{
s.Append(string.Format(HtmlCurrentPage, i));
}
else
{
if (isRewrite)
{
s.Append(string.Format(HtmlPageNum, string.Format(pageUrl, i), i));
}
else
{
if (pageUrl.IndexOf("?") > 0)
s.Append(string.Format(HtmlPageNum, string.Format(pageUrl + @"&pn={0}", i), i));
else
s.Append(string.Format(HtmlPageNum, string.Format(pageUrl + @"?pn={0}", i), i));
}
}
}
s.Append(HtmlNextPage);
if (bGoToPage)
{
s.Append(HTMLMoveToPage);
}
return string.Format(HtmlPageTag, s.ToString());
}
///
/// 获取最终html
///
///
public string GetHtmlNoNumsPage()
{
if (totalPages < 0)
return "";
StringBuilder s = new StringBuilder("");
if (bHtmlCurOfTotalPage)
{
if (string.IsNullOrEmpty(HtmlCurOfTotalPage))
s.Append(string.Format(" 第 {0}/{1} 页 ", currentPage, totalPages));
else
s.Append(string.Format(HtmlCurOfTotalPage, currentPage, totalPages));
}
s.Append(HtmlPrevPage);
s.Append(HtmlNextPage);
return string.Format(HtmlPageTag, s.ToString());
}
}
|