public class PagerHelper
{
private int _pageSize = 10;
/// <summary>
/// 每页默认10条数
/// </summary>
public int pageSize
{
get
{
if (_pageSize < 1)
_pageSize = 1;
return _pageSize;
}
set { _pageSize = value; }
}
private int _extendPage = 5;
/// <summary>
/// 每页显示分页数
/// </summary>
public int extendPage
{
get
{
if (_extendPage < 3)
_extendPage = 3;
return _extendPage;
}
set
{
_extendPage = value;
}
}
private int _currentPage = 1;
/// <summary>
/// 当前页码
/// </summary>
public int currentPage
{
get
{
if (_currentPage < 1)
_currentPage = 1;
if (_currentPage > totalPages)
_currentPage = totalPages;
return _currentPage;
}
set
{
_currentPage = value;
}
}
private int _totalRows = 0;
/// <summary>
/// 总数据数
/// </summary>
public int totalRows
{
get { return _totalRows; }
set { _totalRows = value; }
}
/// <summary>
/// 分页时是否做urlRewriter处理
/// </summary>
private bool isRewrite
{
get { return pageUrl.IndexOf("{0}") > 0; }
}
private string _pageUrl = "";
/// <summary>
/// 分页url
/// </summary>
public string pageUrl
{
get { return _pageUrl; }
set { _pageUrl = value; }
}
/// <summary>
/// 总页数
/// </summary>
private int totalPages
{
get
{
if ((totalRows % pageSize) == 0)
{
return (totalRows / pageSize);
}
return (totalRows / pageSize + 1);
}
}
/// <summary>
/// 上一页
/// </summary>
private int prevPage
{
get
{
int temp = currentPage - 1;
if (temp < 1)
{
return 1;
}
return temp;
}
}
/// <summary>
/// 下一页
/// </summary>
private int nextPage
{
get
{
int temp = currentPage + 1;
if (temp > totalPages)
{
return temp;
}
return temp;
}
}
/// <summary>
/// 获得开始显示的页码
/// </summary>
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;
}
}
/// <summary>
/// 获取最后一个显示的页面
/// </summary>
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 = "";
/// <summary>
/// 第一页时上一页的样式
/// </summary>
public string HtmlDisablePrevPage
{
get { return _HtmlDisablePrevPage; }
set { _HtmlDisablePrevPage = value; }
}
private string _HtmlDisableNextPage = "";
/// <summary>
/// 最后页时下一页的样式
/// </summary>
public string HtmlDisableNextPage
{
get { return _HtmlDisableNextPage; }
set { _HtmlDisableNextPage = value; }
}
private string _HtmlCurOfTotalPage = "";
/// <summary>
/// 当前第几页 例如:第 2/5 页
/// </summary>
public string HtmlCurOfTotalPage
{
get { return _HtmlCurOfTotalPage; }
set { _HtmlCurOfTotalPage = value; }
}
private bool _bHtmlCurOfTotalPage = true;
/// <summary>
/// 当前第几页 例如:第 2/5 页
/// </summary>
public bool bHtmlCurOfTotalPage
{
get { return _bHtmlCurOfTotalPage; }
set { _bHtmlCurOfTotalPage = value; }
}
private string _HtmlTotalCount = "";
/// <summary>
/// 总记录数的显示形式
/// </summary>
public string HtmlTotalCount
{
get { return _HtmlTotalCount; }
set { _HtmlTotalCount = value; }
}
private string _HTMLMoveToPage = "";
/// <summary>
/// 跳转到的格式HTML
/// </summary>
public string HTMLMoveToPage
{
get
{
string strReturnHtml = string.Format(_HTMLMoveToPage, currentPage);
if (isRewrite)
{
strReturnHtml += "<script>function GoToPage(pg){if(isNaN(pg)||pg>" + totalPages + "||pg<1){window.alert('请正确输入页码,只能为正整数!且不能大于" + totalPages + "');return false;}else{window.location.href='"+pageUrl+"'.replace('{0}',pg);return true;}}</script>";
}
else
{
if (pageUrl.IndexOf("?") > 0)
strReturnHtml += "<script>function GoToPage(pg){if(isNaN(pg)||pg>" + totalPages + "||pg<1){window.alert('请正确输入页码,只能为正整数!且不能大于" + totalPages + "');return false;}else{window.location.href='" + pageUrl + "&pn='+pg+'';return true;}}</script>";
else
strReturnHtml += "<script>function GoToPage(pg){if(isNaN(pg)||pg>" + totalPages + "||pg<1){window.alert('请正确输入页码,只能为正整数!且不能大于" + totalPages + "');return false;}else{window.location.href='" + pageUrl + "?pn='+pg+'';return true;}}</script>";
}
return strReturnHtml;
}
set
{
_HTMLMoveToPage = value;
}
}
private string _HtmlPrevPage = @"";
/// <summary>
/// 上一页代码
/// </summary>
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 = @"";
/// <summary>
/// 下一页html代码
/// </summary>
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 = @"<span style=""color:Red"">{0}</span> ";
/// <summary>
/// 当前页html代码
/// </summary>
public string HtmlCurrentPage
{
get { return _HtmlCurrentPage; }
set { _HtmlCurrentPage = value; }
}
private string _HtmlPageNum = "";
/// <summary>
/// 页码数的html
/// </summary>
public string HtmlPageNum
{
get { return _HtmlPageNum; }
set { _HtmlPageNum = value; }
}
private string _HtmlPageTag = @"<div class=""page""><ul>{0}</ul></div>";
/// <summary>
/// 用于设置分页html外面的div, span等标
/// </summary>
public string HtmlPageTag
{
get { return _HtmlPageTag; }
set { _HtmlPageTag = value; }
}
/// <summary>
/// 获取最终html
/// </summary>
/// <returns></returns>
public string GetHtmlPage()
{
return GetHtmlPage(false);
}
/// <summary>
/// 获取最终html
/// </summary>
/// <returns></returns>
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());
}
/// <summary>
/// 获取最终html
/// </summary>
/// <returns></returns>
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());
}
}
|