asp.net mvc 的几种分页Pager

   第一种

   /// <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
    }
}
时间: 2024-10-07 21:46:59

asp.net mvc 的几种分页Pager的相关文章

在 asp.net mvc中的简单分页算法

//第一步:建立如下分页实体类:namespace MVCPager.Helpers { /// <summary> /// 简单分页算法类 /// </summary> public class Pager { public int RecordCount { get; set; } public int PageIndex { get; set; } public int PageSize { get; set; } public int PageCount { get { r

ASP.NET MVC Filters 4种默认过滤器的使用【附示例】

http://www.cnblogs.com/oppoic/p/mvc_authorization_action_result_exception_filters.html ASP.NET MVC Filters 4种默认过滤器的使用[附示例] 过滤器(Filters)的出现使得我们可以在ASP.NET MVC程序里更好的控制浏览器请求过来的URL,不是每个请求都会响应内容,只响应特定内容给那些有特定权限的用户,过滤器理论上有以下功能: 判断登录与否或用户权限 决策输出缓存 防盗链 防蜘蛛 本地

ASP.NET MVC中有四种过滤器类型

在ASP.NET MVC中有四种过滤器类型 Action 1.在ASP.NET MVC项目中,新建文件夹Filter,然后新建类MyCustormFilter,继承自ActionFilterAttribute类,我们来看下ActionFilterAttribute类有如下四个方法,从命名我应该就可以看出他们的执行时机. public class MyCustormFilter:ActionFilterAttribute { public override void OnActionExecuti

认识ASP.NET MVC的5种AuthorizationFilter

在总体介绍了筛选器及其提供机制(<深入探讨ASP.NET MVC的筛选器>)之后,我们按照执行的先后顺序对四种不同的筛选器进行单独介绍,首先来介绍最先执行的AuthorizationFilter.从命名来看,AuthorizationFilter用于完成授权相关的工作,所以它应该在Action方法被调用之前执行才能起到授权的作用.不仅限于授权,如果我们希望目标Action方法被调用之前中断执行的流程"做点什么",都可以以AuthorizationFilter的形式来实现.[

Asp.Net中的三种分页方式总结

本人ASP.net初学,网上找了一些分页的资料,看到这篇文章,没看到作者在名字,我转了你的文章,只为我可以用的时候方便查看,2010的文章了,不知道这技术是否过期. 以下才是正文 通常分页有3种方法,分别是asp.net自带的数据显示空间如GridView等自带的分页,第三方分页控件如aspnetpager,存储过程分页等.这里分别做总结. 第一种:使用GridView自带分页,这种是最简单的分页方法. 前台的方法: <asp:GridView ID="GridView1" Al

ASP.NET中的几种分页

1.通过DataGrid控件实现分页 ASP.NET中的DataGrid控件自带了分页功能,当绑定了DataGrid的数据源之后,须要对DataGrid控件进行一些设置: 左击控件右上角的小箭头→属性生成器: 选择左边的[分页]选项卡 选中[同意分页].[页大小]表示一页显示多少条数据:导航button的[位置]有顶.底.上下型三种选择.[模式]有页码和"上一页.下一页button".假设选择了页码模式.[数值button]表示最多显示的button数量,假设选择了还有一种模式,&qu

Asp.Net MVC EasyUI DataGrid查询分页

function doSearch() { //查询方法 var searchValue = $('#txtQueryTC001').textbox('getText'); $('#dgCMSTC').datagrid('options').queryParams = { condition: searchValue }; //initDataGrid(searchValue); $.ajax({ type: 'post', url: '/CMSTC/GetJson', data: 'condi

7.asp.net mvc EF四种方式的传值方式

2.添加视图:view2 运行效果:

ASP.NET MVC分页实现

ASP.NET MVC中不能使用分页控件,所以我就自己写了一个分页局部视图,配合PageInfo类,即可实现在任何页面任意位置呈现分页,由于采用的是基于POST分页方式,所以唯一的限制就是必须放在FORM中,当然以后我会考虑实现基于URL分页的! 一.PageInfo类 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 6 namespace ROIS.Mo