ASP.NET自定义分页程序代码

作者:袖梨 2022-06-25

一、自定义分页类 PagerHelper

 代码如下 复制代码

 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 = @"

    {0}
";
///
/// 用于设置分页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());
}

    }

 


二、使用实例
 

 代码如下 复制代码

public string GetHtmlPager()
{
    PagerHelper pHelper = new PagerHelper();
    pHelper.HtmlPageTag = @"

    {0}
";
    pHelper.pageUrl = url;
    pHelper.totalRows = TotalCount;
    pHelper.pageSize = pageSize;
    pHelper.extendPage = 8;
    pHelper.HtmlPrevPage = @"
  • 上一页
  • ";
        pHelper.HtmlNextPage = @"
  • 下一页
  • ";
        pHelper.HtmlDisablePrevPage = @"
  • 上一页
  • ";
        pHelper.HtmlDisableNextPage = @"
  • 下一页
  • ";
        pHelper.HtmlCurrentPage = @"
  • {0}
  • ";
        pHelper.currentPage = pageIndex;
        pHelper.HtmlPageNum = @"
  • {1}
  • ";
        return pHelper.GetHtmlPage();
    }


    自定义分页控件

    使用:

     代码如下 复制代码
    <%@ Register assembly="YSM.AspNetPager" namespace="YSM.AspNetPager" tagprefix="cc1" %>

    页面注册控件,也可以在web.config中配置
    1.ajax之UpdatePanel分页则把控件放到UpdatePanel里面即可(不能启用url重写和url参数分页,即跳转页面)

     代码如下 复制代码

    CssClass:设置控件div的样式;CurrentPageButtonClass:设置当前索引页的样式;DisabledButtonClass:设置启用上一页下一页按钮时的禁用样式。
    2.url重写分页需要设置UrlRewriting属性为true,Url重写规则UrlRewritePattern属性。

     代码如下 复制代码

    index-{0}-[cid].html多个参数时候的使用页面使用{0}自动格式化,其他参数使用[你的参数名]进行配置。
    属性介绍:
    PageSize:获取或设置每页显示记录数
    CurrentPageIndex:获取或设置当前页索引
    RecordCount:获取或设置记录总数(必须设置值才能生成分页链接)
    PageCount:获取页总数,此属性只有get
    UrlPaging:是否启用Url传参方式分页(默认false)
    UrlPageIndexName:"获取或设置当启用Url分页方式时,在url中表示要传递的页索引的参数的名称(默认page)。
    UrlRewriting:是否启用Url重写,当为true时UrlPaging自定为true(默认false)
    UrlRewritePattern:Url重写格式(默认null)
    CurrentPageButtonClass:当前页按钮样式(默认null)
    DisabledButtonClass:设置禁用上一页下一页按钮样式(默认null)
    FirstPageText:设置第一页按钮的文本(默认1)
    LastPageText:设置最后一夜按钮的文本(默认PageCount的值)
    ShowJumpBox:设置是否显示跳转框(默认显示)

    相关文章

    精彩推荐