还是围绕新项目来的,因为没有统一的分页控件,大家各写各的,浪费时间不说,长的又千奇百怪,所以就把这个小活收回来了,乱七八糟写了一通,还凑合能用。主要逻辑就是根据当前页码分页大小记录总数计算前两页后两页,以及上、下页等对应的链接地址就可以了。代码保存一下,以备后续完善。因公司不能上传图片,所以大家只好来个亲自动手看一下效果了。

分页控件需要读取url中的“page”参数,在页面中分页的位置写下以下语句就可以了

<%=******.Utility.Pager.Html(this.TotalCount, this.Page, 10) %>

CS代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace ******.Utility
{
    /// <summary>
    /// 分页
    /// </summary>
    public class Pager
    {
        ///<summary>
        ///分页控件
        ///</summary>
        ///<param name=”recordCount”>记录总数</param>
        ///<param name=”pageIndex”>分页索引</param>
        ///<param name=”pageSize”>分页大小</param>       
        /// <returns>返回分页的Html代码</returns>
        public static string Html(int recordCount, int pageIndex, int pageSize)
        {
            string result = string.Empty;

            pageSize = pageSize < 1 ? 10 : pageSize;
             pageIndex = pageIndex < 1 ? 1 : pageIndex;
             recordCount = recordCount < 1 ? 1 : recordCount;
 
            if (recordCount > pageSize)
             {
                 int pageCount = (recordCount % pageSize) == 0 ? (recordCount / pageSize) : (recordCount / pageSize) + 1;
 
                string pageLink = PageLinkConstruct();
 
                if (pageIndex > pageCount)
                 {
                     pageIndex = pageCount;
                 }
 
                string htmlLeftPage = string.Empty;
                 string htmlRightPage = string.Empty;
 
                //前两页
                 for (int i = 2; i > 0; i–)
                 {
                     if (pageIndex – i > 0)
                     {
                         htmlLeftPage += string.Format(“<a href=\”{0}\”>{1}</a>”, string.Format(pageLink, (pageIndex – i)), pageIndex – i);
                     }
                 }
                 //后两页
                 for (int j = 1; j < 3; j++)
                 {
                     if (pageIndex + j <= pageCount)
                     {
                         htmlRightPage += string.Format(“<a href=\”{0}\”>{1}</a>”, string.Format(pageLink, (pageIndex + j)), pageIndex + j);
                     }
                 }
 
                int prevPage = pageIndex – 1;
 
                if (prevPage < 1)
                 {
                     prevPage = 1;
                 }
 
                int nextPage = pageIndex + 1;
 
                if (nextPage > pageCount)
                 {
                     nextPage = pageCount;
                 }
 
                string leftHtml = string.Empty;
 
                if (pageIndex > 1)
                 {
                     leftHtml = string.Format(“<a {0}>{1}</a><a {2}>{3}</a>{4}”, string.Format(“href=\”{0}\””, string.Format(pageLink, 1)), “首页”, string.Format(“href=\”{0}\””, string.Format(pageLink, prevPage)), “上一页”, htmlLeftPage);
                 }
 
                string rightHtml = string.Empty;
 
                if (pageIndex < pageCount)
                 {
                     rightHtml = string.Format(“{0}<a {1}>{2}</a><a {3}>{4}</a>”, htmlRightPage, string.Format(“href=\”{0}\””, string.Format(pageLink, nextPage)), “下一页”, string.Format(“href=\”{0}\””, string.Format(pageLink, pageCount)), “尾页”);
                 }
 
                result = string.Format(“<div class=\”htmlpager\”>{0}<b>{1}</b>{2}</div>”, leftHtml, pageIndex, rightHtml);              
             }
 
            return result;

}

        /// <summary>
        /// 构造翻页Url
        /// </summary>
        /// <returns></returns>
        private static string PageLinkConstruct()
        {
            HttpContext hc = HttpContext.Current;

            StringBuilder sb = new StringBuilder();

            if (!string.IsNullOrEmpty(hc.Request.QueryString[“page”]))
            {
                for (int i = 0; i < hc.Request.QueryString.Count; i++)
                {
                    if (hc.Request.QueryString.Keys[i] != “page”)
                    {
                        sb.AppendFormat(“{0}={1}”, hc.Request.QueryString.Keys[i], hc.Request.QueryString[i].ToString());
                        sb.Append(“&”);
                    }
                    else
                    {
                        sb.Append(“page={0}&”);
                    }
                }
            }
            else
            {
                for (int i = 0; i < hc.Request.QueryString.Count; i++)
                {
                    sb.AppendFormat(“{0}={1}”, hc.Request.QueryString.Keys[i], hc.Request.QueryString[i].ToString());
                    sb.Append(“&”);
                }

                sb.Append(“page={0}&”);
            }

            if (sb.Length > 0)
            {
                sb.Remove(sb.Length – 1, 1);
            }

            var url = new UriBuilder();

            url.Scheme = hc.Request.Url.Scheme;
            url.Host = hc.Request.Url.Host;
            url.Port = hc.Request.Url.Port;
            url.Path = hc.Request.Url.AbsolutePath;
            url.Query = sb.ToString();

            return url.ToString();
        }
    }
}

CSS

DIV.htmlpager { MARGIN-TOP: 20px; MARGIN-BOTTOM: 10px}
DIV.htmlpager A {BORDER-RIGHT: #8db5d7 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #8db5d7 1px solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 2px; BORDER-LEFT: #8db5d7 1px solid; COLOR: #000; MARGIN-RIGHT: 2px; PADDING-TOP: 2px; BORDER-BOTTOM: #8db5d7 1px solid; TEXT-DECORATION: none}
DIV.htmlpager A:hover {BORDER-RIGHT: red 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: red 1px solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 2px; BORDER-LEFT: red 1px solid; MARGIN-RIGHT: 2px; PADDING-TOP: 2px; BORDER-BOTTOM: red 1px solid}
DIV.htmlpager A:active {BORDER-RIGHT: red 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: red 1px solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 2px; BORDER-LEFT: red 1px solid; MARGIN-RIGHT: 2px; PADDING-TOP: 2px; BORDER-BOTTOM: red 1px solid}
DIV.htmlpager b {BORDER-RIGHT: #e89954 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #e89954 1px solid; PADDING-LEFT: 5px; FONT-WEIGHT: bold; PADDING-BOTTOM: 2px; BORDER-LEFT: #e89954 1px solid; COLOR: #000; MARGIN-RIGHT: 2px; PADDING-TOP: 2px; BORDER-BOTTOM: #e89954 1px solid; BACKGROUND-COLOR: #ffca7d}

版权声明:本文为EddyPeng原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/EddyPeng/archive/2012/08/21/2648546.html