码迷,mamicode.com
首页 > Web开发 > 详细

asp.net mvc 的几种分页Pager

时间:2015-01-27 14:47:56      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:

   第一种

   /// <summary>

         /// 分页Pager显示
        /// </summary>
        /// <param name="html"></param>
        /// <param name="currentPageStr">标识当前页码的QueryStringKey</param>
        /// <param name="pageSize">每页显示</param>
        /// <param name="totalCount">总数据量</param>
        /// <returns></returns>
        public static string Pager(this HtmlHelper html, string currentPageStr, int pageSize, int totalCount)
        {
            var queryString = html.ViewContext.HttpContext.Request.QueryString;
            int currentPage = 1; //当前页
            int.TryParse(queryString[currentPageStr], out currentPage); //与相应的QueryString绑定
            var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1); //总页数
            var dict = new RouteValueDictionary(html.ViewContext.RouteData.Values);

            var output = new StringBuilder();

            foreach (string key in queryString.Keys)
                if (queryString[key] != null && !string.IsNullOrEmpty(key))
                    dict[key] = queryString[key];
            if (totalPages > 1)
            {
                if (currentPage != 1)
                {//处理首页连接
                    dict["p"] = 1;
                    output.AppendFormat("{0} ", html.RouteLink("首页", dict));
                }
                if (currentPage > 1)
                {//处理上一页的连接
                    dict["p"] = currentPage - 1;
                    output.Append(html.RouteLink("上一页", dict));
                }
                else
                {
                    output.Append("上一页");
                }
                output.Append(" ");
                int currint = 5;
                for (int i = 0; i <= 10; i++)
                {//一共最多显示10个页码,前面5个,后面5个
                    if ((currentPage + i - currint) >= 1 && (currentPage + i - currint) <= totalPages)
                        if (currint == i)
                        {//当前页处理
                            output.Append(string.Format("[{0}]", currentPage));
                        }
                        else
                        {//一般页处理
                            dict["p"] = currentPage + i - currint;
                            output.Append(html.RouteLink((currentPage + i - currint).ToString(), dict));
                        }
                    output.Append(" ");
                }
                if (currentPage < totalPages)
                {//处理下一页的链接
                    dict["p"] = currentPage + 1;
                    output.Append(html.RouteLink("下一页", dict));
                }
                else
                {
                    output.Append("下一页");
                }
                output.Append(" ");
                if (currentPage != totalPages)
                {
                    dict["p"] = totalPages;
                    output.Append(html.RouteLink("末页", dict));
                }
                output.Append(" ");
            }
            output.AppendFormat("{0} / {1}", currentPage, totalPages);//这个统计加不加都行
            return output.ToString();
        }
第二个

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

namespace System.Web.Mvc
{
    public static class PagerHelper
    {
        /// 
        /// 分页
        /// 
        /// 
        /// 分页id
        /// 当前页
        /// 分页尺寸
        /// 记录总数
        /// 分页头标签属性
        /// 分页样式
        /// 分页模式
        /// 
        public static string Pager(this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount, object htmlAttributes, string className, PageMode mode)
        {
            TagBuilder builder = new TagBuilder("div");
            builder.IdAttributeDotReplacement = "_";
            builder.GenerateId(id);
            builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
            builder.InnerHtml = GetNormalPage(helper, currentPageIndex, pageSize, recordCount, mode);
            return builder.ToString(TagRenderMode.Normal);
        }

        /// 
        /// 分页
        /// 
        /// 
        /// 分页id
        /// 当前页
        /// 分页尺寸
        /// 记录总数
        /// 分页样式
        /// 
        public static string Pager(this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount, string className)
        {
            return Pager(helper, id, currentPageIndex, pageSize, recordCount, null, className, PageMode.Normal);
        }

        /// 
        /// 分页
        /// 
        /// 
        /// 分页id
        /// 当前页
        /// 分页尺寸
        /// 记录总数
        /// 
        public static string Pager(this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount)
        {
            return Pager(helper, id, currentPageIndex, pageSize, recordCount, null);
        }

        /// 
        /// 分页
        /// 
        /// 
        /// 分页id
        /// 当前页
        /// 分页尺寸
        /// 记录总数
        /// 分页模式
        /// 
        public static string Pager(this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount, PageMode mode)
        {
            return Pager(helper, id, currentPageIndex, pageSize, recordCount, null, mode);
        }

        /// 
        /// 分页
        /// 
        /// 
        /// 分页id
        /// 当前页
        /// 分页尺寸
        /// 记录总数
        /// 分页样式
        /// 分页模式
        /// 
        public static string Pager(this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount, string className, PageMode mode)
        {
            return Pager(helper, id, currentPageIndex, pageSize, recordCount, null, className, mode);
        }

        /// 
        /// 获取普通分页
        /// 
        /// 
        /// 
        /// 
        /// 
        private static string GetNormalPage(this HtmlHelper helper, int currentPageIndex, int pageSize, int recordCount, PageMode mode)
        {
            int pageCount = (recordCount % pageSize == 0 ? recordCount / pageSize : recordCount / pageSize + 1);
            string url = HttpContext.Current.Request.Url.AbsolutePath + "?page={0}";
            string HtmlStr = string.Empty;
            NameValueCollection collection = HttpContext.Current.Request.QueryString;
            string[] keys = collection.AllKeys;
            for (int i = 0; i < keys.Length; i++)
            {
                if (keys[i].ToLower() != "page")
                    url += "&" + keys[i] + "=" + collection[keys[i]] + "";
            }
            HtmlStr += "<div class=\"right maunlist\" style=\"width: 520px; float:right;\">";
            HtmlStr += "<div class=\"left manur\" style=\"float:right\">";
            HtmlStr += "<div class=\"manudisableda\">到第</div>";
            HtmlStr += "<div class=\"manudisableda\"><input name=\"\" type=\"text\" id=\"page\" style=\"width:40px; margin-top:2px\"/></div>";
            HtmlStr += "<div class=\"manudisableda\">页</div><div class=\"manudisableda\"><a href=\"#\"><img id=\"queding\" src=\"../../list_img/OKBTN.jpg\" width=\"70\"  height=\"24\" /></a></div></div>";
           // HtmlStr += "<tr><td>总共" + recordCount + "条记录,共" + pageCount + "页,当前第" + currentPageIndex + "页  </td>";
            HtmlStr += "<div class=\"left manuleft\" style=\"float:right;\">";
            //if (currentPageIndex == 1)
            //    HtmlStr += "<a>首页</a> ";
            //else
            //{
            //    string url1 = string.Format(url.ToString(), 1);
            //    HtmlStr += "<a href=\"" + url1 + "\">首页</a> ";
            //}
            if (currentPageIndex > 1)
            {
                string url1 = string.Format(url.ToString(), currentPageIndex - 1);
                HtmlStr += "<a href=\"" + url1 + "\">上一页</a> ";

            }
            else
                HtmlStr += "<span class=\"pageNum\">上一页</span> ";
            if (mode == PageMode.Numeric)
                HtmlStr += GetNumericPage(currentPageIndex, pageSize, recordCount, pageCount, url.ToString());
            if (currentPageIndex < pageCount)
            {
                string url1 = string.Format(url.ToString(), currentPageIndex + 1);
                HtmlStr += "<a href=\"" + url1 + "\">下一页</a> ";
            }
            else
                HtmlStr += "<span class=\"pageNum\">下一页</span> ";

            //if (currentPageIndex == pageCount)
            //    HtmlStr += "末页 ";
            //else
            //{
            //    string url1 = string.Format(url.ToString(), pageCount);
            //    HtmlStr += "<a href=\"" + url1 + "\">末页</a> ";
            //}
            HtmlStr += "</div>";
            
            HtmlStr += "</div>";
            return HtmlStr.ToString();
        }

        /// 
        /// 获取数字分页
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        private static string GetNumericPage(int currentPageIndex, int pageSize, int recordCount, int pageCount, string url)
        {
            int page = 5;
            
            int count = pageCount % page == 0 ? pageCount / page : (pageCount / page + 1);
            int k = currentPageIndex / page;
            int m = currentPageIndex % page;
            string sbhtml = string.Empty;
            //当前在第几个分页上
            int index = currentPageIndex % page == 0 ? currentPageIndex / page : (currentPageIndex / page + 1);
            if (index != 1)
            {
                sbhtml += "<span>...</span>";
            }
            if (currentPageIndex / page == pageCount / page)
            {
                if (m == 0)
                {
                    k--;
                    m = page;
                }
                else
                    m = pageCount % page;
            }
            else
                if (m == 0)
                {
                    k--;
                    m = page;
                }
                else
                {
                    m = 5;
                }

            for (int i = k * page + 1; i <= k * page + m; i++)
            {
                if (i == currentPageIndex)
                    sbhtml += "<font class=\"pagertion\">" + string.Format("{0} ", i) + "</font>";
                else
                {
                    string url1 = string.Format(url.ToString(), i);
                    sbhtml += "<a href=\"" + url1 + "\">" + string.Format("{1} ", url1, i) + "</a>";
                    
                }
                
               
            }

            if (count > 1)
            {
                if (index < count)
                {
                    sbhtml += "<span>...</span>";
                }


            }

            return sbhtml.ToString();
        }
    }

    /// 
    /// 分页模式
    /// 
    public enum PageMode
    {
        /// 
        /// 普通分页模式
        /// 
        Normal,
        /// 
        /// 普通分页加数字分页
        /// 
        Numeric
    }
}

 

asp.net mvc 的几种分页Pager

标签:

原文地址:http://www.cnblogs.com/yiliuyang/p/4252710.html

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