前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >c# asp.net 实现分页(pager)功能

c# asp.net 实现分页(pager)功能

作者头像
纯粹是糖
发布2018-03-14 15:03:25
2.4K0
发布2018-03-14 15:03:25
举报
文章被收录于专栏:kwcode

分页PagerHelper辅助类

代码语言:javascript
复制
using System;
using System.Web;
代码语言:javascript
复制
public class PagerHelper
{
    #region 获取分页的Html代码
    /// <summary>
    /// 获取分页的Html代码
    /// 当前页码方法内部根据Request["page"]获取
    /// </summary>
    /// <param name="pageSize">每一页数量</param>
    /// <param name="totalCount">总数量</param>
    /// <param name="url">伪静态地址如/news/list-1-{0}.html</param>
    /// <param name="maxPageNum">最多显示的页码个数(100页 每次只显示8个其他隐藏)</param>
    /// <returns></returns>
    public static string GetPageHtml(int pageSize, int totalCount, string url, int maxPageNum = 8)
    {
        HttpRequest Request = HttpContext.Current.Request;
        int curPageIndex = 1;
        if (!string.IsNullOrWhiteSpace(Request["page"]))
        {
            curPageIndex = Convert.ToInt32(Request["page"] ?? "1");
            curPageIndex = curPageIndex <= 0 ? 1 : curPageIndex;
        }

        System.Text.StringBuilder pageHtml = new System.Text.StringBuilder();
        //if (pageIndex > 1)
        //{
        pageHtml.Append(curPageIndex == 1 ? "<a href=\"javascript:void(0);\">首页</a>" : "<a   href=\"" + string.Format(url, 1) + "\">首页</a>");
        pageHtml.Append(curPageIndex > 1 ? "<a href=\"" + string.Format(url, curPageIndex - 1) + "\">上一页</a>" : "<a href=\"javascript:void(0);\">上一页</a>");
        //}
        int pageCount = GetPageCount(pageSize, totalCount);//总页码
        //获取显示区域第一个开始位置 如 1 9 17
        int firstNum = curPageIndex % maxPageNum == 0 ? curPageIndex - (maxPageNum - 1) : curPageIndex - curPageIndex % maxPageNum + 1;
        if (firstNum > maxPageNum)
        {
            pageHtml.Append("<a   href=\"" + string.Format(url, firstNum - 1) + "\">...</a>");
        }

        for (int i = firstNum; i < firstNum + maxPageNum; i++)
        {
            if (i > pageCount) break;
            string css = string.Empty;
            if (i == curPageIndex)
            {
                css = "class=\"currentpage\"";
            }
            pageHtml.Append("<a " + css + " href=\"" + string.Format(url, i) + "\">" + i + "</a>");

        }
        if (pageCount >= firstNum + maxPageNum)
        {
            pageHtml.Append("<a   href=\"" + string.Format(url, firstNum + maxPageNum) + "\">...</a>");
        }
        //if (pageCount > curPageIndex)
        //{
        pageHtml.Append(curPageIndex < pageCount ? "<a href=\"" + string.Format(url, curPageIndex + 1) + "\">下一页</a>" : "<a href=\"javascript:void(0);\">下一页</a>");
        pageHtml.Append("<a href=\"" + string.Format(url, pageCount) + "\">尾页</a>");
        //}
        pageHtml.Append(string.Format("<a href=\"javascript:void(0);\">共{0}页,{1}条</a>", pageCount, totalCount));
        return pageHtml.ToString();
    }

    #endregion

    #region 获取页码总数
    /// <summary>
    /// 获取页码总数
    /// </summary>
    /// <param name="pageSize">每一页 数量</param>
    /// <param name="totalCount">总数量</param>
    /// <returns></returns>
    private static int GetPageCount(int pageSize, int totalCount)
    {
        int pageNumbers = 0;
        if (totalCount % pageSize != 0)
        {
            pageNumbers = totalCount / pageSize + 1;
        }
        else
        {
            pageNumbers = totalCount / pageSize;
        }
        pageNumbers = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(totalCount) / Convert.ToDouble(pageSize)));
        return pageNumbers;
    }
    #endregion

}

  前台使用

代码语言:javascript
复制
<%@ Page Language="C#" AutoEventWireup="true" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <style>
        html, body, div, span, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, code, del, dfn, em, img, q, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, i { margin: 0; padding: 0; border: 0; font-weight: inherit; font-style: inherit; font-size: 100%; font-family: inherit; vertical-align: baseline; }
        body { background: #fff; font: 12px/1.5 Tahoma; color: #000; }
        a { text-decoration: none; cursor: pointer; }
        /*分页*/
        .page { clear: both; text-align: center; margin-top: 10px; margin-bottom: 20px; }
        .page a { border: 1px solid #dbdbdb; background: #fff; padding: 5px 10px; margin: 1px; display: inline-block; color: #000; }
        .page a:hover { text-decoration: none; background-color: #2196F3; color: #fff; }
        .page span a { border: 1px solid #1f5b13; background: #fff; padding: 2px 7px; margin: 1px; display: inline-block; color: #104c00; }
        .page span a:hover { text-decoration: none; background-color: #a3c79c; }
        .page .currentpage { background-color: #ff8800; color: #fff; }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div class="page">
            <asp:Literal runat="server" ID="ltHtml"></asp:Literal>
        </div>
    </form>
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            ltHtml.Text = PagerHelper.GetPageHtml(3, 33, "/tpager_demo.aspx?page={0}");
        }
    </script>
</body>
</html>

预览图  

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档