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

分页PagerHelper辅助类

using System;
using System.Web;
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

}

  前台使用

<%@ 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>

预览图  

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏飞扬的花生

合并两个结构完全相同的DataTable

两个结构一模一样的DataTable如何合并? 例子:使用Winform进行演示,表2的数据为固定的,表1的数据可以动态添加,通过合并按钮合并表1和表2的数据到...

1685
来自专栏张善友的专栏

Visual Studio 2005的各个版本

支持正版,做DotNet开发,工具选择不二是Vistual Studio 2005,需要了解各个版本的内容相关的Licence: A:Visual Stud...

2067
来自专栏乐沙弥的世界

使用yum快速部署Oracle安装环境(11g)

    基于Linux安装过Oracle的童鞋们都应该清楚,安装Oracle的确是一件比较费时费力的差事,因为仅仅是前期的rpm包,内核参数,创建用户等等这些个...

1681
来自专栏后台及大数据开发

CentOS下redis集群安装

环境: 一台CentOS虚拟机上部署六个节点,创建3个master,3个slave节点

632
来自专栏小樱的经验随笔

Codeforces 839B Game of the Rows【贪心】

B. Game of the Rows time limit per test:1 second memory limit per test:256 megab...

3176
来自专栏逢魔安全实验室

Some Linux Hacking Tricks

3415
来自专栏数据库新发现

Use bonnie++ to Test system IO speed[修正稿]

http://www.eygle.com/unix/Use.Bonnie++.To.Test.IO.speed.htm

732
来自专栏鬼谷君

CentOS系统自动下载RPM包及其所有依赖的包

1473
来自专栏hbbliyong

5步搭建GO环境

Easy Go Programming Setup for Windows Dec 23, 2014 I’ve had to do this more t...

3707
来自专栏点点滴滴

windows平台KMS激活秘钥

1243

扫码关注云+社区