码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2017-08-02 00:34:04      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:ati   img   space   frame   current   cap   enter   web   splay   

分页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>

预览图  

技术分享

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

标签:ati   img   space   frame   current   cap   enter   web   splay   

原文地址:http://www.cnblogs.com/LoveTX/p/7271565.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!