码迷,mamicode.com
首页 > 其他好文 > 详细

分页“真分页”获取数据

时间:2017-03-06 22:08:33      阅读:295      评论:0      收藏:0      [点我收藏+]

标签:htmlhelp   pen   for   text   html   static   span   web   class   

对于初学的新手来说,做数据分页的时候,经常是去网上下载个控件,所谓控件分页,有的人认为很好,因为方便,但是对于老鸟来说

控件分页查询效率太低,在实际工作中,也会带来致命的弱点,所以,我们一般采用真分页,部分查询,效率没得说!!

石家庄ReactNative交流群 526955818欢迎大家加入

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
//命名空间换成引用它的类的同一个层次
namespace System.Web.Mvc
{
    public static class MyHtmlPage
    {
        /// <summary>
        ///
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="currentPage"></param>
        /// <param name="sizePage"></param>
        /// <param name="totalPage"></param>
        /// <returns></returns>
        public static HtmlString ShowPageNavigate(this HtmlHelper htmlHelper,int currentPage,int pageSize,int totalCount)
        {
            //获取重定向的url
            var redierctTo = htmlHelper.ViewContext.RequestContext.HttpContext.Request.Url.AbsolutePath;
            pageSize = pageSize == 0 ? 3 : pageSize;
            var totalPages = Math.Max((totalCount + pageSize - 1)/pageSize, 1);
            var output=new StringBuilder();
            if (currentPage > 1)
            {
                if (currentPage!=1)
                {
                    //处理首页链接
                    output.AppendFormat("<a class=‘pageLink‘ href=‘{0}?pageIndex=1&pageSize={1}‘>首页</a>", redierctTo,
                        pageSize);
                }
                if (currentPage>1)
                {
                    //处理上一页链接
                    output.AppendFormat("<a class=‘pageLink‘ href=‘{0}?pageIndex={1}&pageSize={2}‘>上一页</a>", redierctTo,currentPage-1,
                        pageSize);

                }
                else
                {
                    output.AppendFormat("<span class=‘pageLink‘>上一页</span>");
                }
                output.Append(" ");
                int currint = 5;
                for (int i = 0; i < 10; i++)
                {
                    //一页最多显示10个页码 前五 后五
                    if ((currentPage+i-currint>=1&&(currentPage+1-currint<=totalPages)))
                    {
                        
                    }
                    else
                    {
                        if (currint==1)
                        {
                            //处理当前页
                            output.AppendFormat("<a class=‘cpb‘ href=‘{0}?pageIndex={1}&pageSize={2}‘>{3}</a>",redierctTo,currentPage,pageSize,currentPage);

                        }
                        else
                        {
                            //一般页处理
                            output.AppendFormat("<a class=‘pageLink‘ href=‘{0}?pageIndex={1}&pageSize={2}‘>{3}</a>", redierctTo, currentPage+i-currint, pageSize, currentPage+i-currint);

                        }
                    }
                    output.Append(" ");
                }
                if (currentPage<totalPages)
                {
                    //处理下一页链接
                    output.AppendFormat("<a class=‘pageLink‘ href=‘{0}?pageIndex={1}&pageSize={2}‘>下一页</a>", redierctTo, currentPage+1,
                        pageSize);
                }
                else
                {
                    //为啥都注释了?
                    output.AppendFormat("<span class=‘pageLink‘>下一页</span>");
                }
                output.Append(" ");

            }
            output.AppendFormat("第{0}页/第{1}页",currentPage,totalPages);//加不加这个统计工具都行
            return new HtmlString(output.ToString());

        }//分页样式代码搞定
    }
}
//注意下面是分页的样式css

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
//命名空间换成引用它的类的同一个层次
namespace System.Web.Mvc
{
    public static class MyHtmlPage
    {
        /// <summary>
        ///
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="currentPage"></param>
        /// <param name="sizePage"></param>
        /// <param name="totalPage"></param>
        /// <returns></returns>
        public static HtmlString ShowPageNavigate(this HtmlHelper htmlHelper,int currentPage,int pageSize,int totalCount)
        {
            //获取重定向的url
            var redierctTo = htmlHelper.ViewContext.RequestContext.HttpContext.Request.Url.AbsolutePath;
            pageSize = pageSize == 0 ? 3 : pageSize;
            var totalPages = Math.Max((totalCount + pageSize - 1)/pageSize, 1);
            var output=new StringBuilder();
            if (currentPage > 1)
            {
                if (currentPage!=1)
                {
                    //处理首页链接
                    output.AppendFormat("<a class=‘pageLink‘ href=‘{0}?pageIndex=1&pageSize={1}‘>首页</a>", redierctTo,
                        pageSize);
                }
                if (currentPage>1)
                {
                    //处理上一页链接
                    output.AppendFormat("<a class=‘pageLink‘ href=‘{0}?pageIndex={1}&pageSize={2}‘>上一页</a>", redierctTo,currentPage-1,
                        pageSize);

                }
                else
                {
                    output.AppendFormat("<span class=‘pageLink‘>上一页</span>");
                }
                output.Append(" ");
                int currint = 5;
                for (int i = 0; i < 10; i++)
                {
                    //一页最多显示10个页码 前五 后五
                    if ((currentPage+i-currint>=1&&(currentPage+1-currint<=totalPages)))
                    {
                        
                    }
                    else
                    {
                        if (currint==1)
                        {
                            //处理当前页
                            output.AppendFormat("<a class=‘cpb‘ href=‘{0}?pageIndex={1}&pageSize={2}‘>{3}</a>",redierctTo,currentPage,pageSize,currentPage);

                        }
                        else
                        {
                            //一般页处理
                            output.AppendFormat("<a class=‘pageLink‘ href=‘{0}?pageIndex={1}&pageSize={2}‘>{3}</a>", redierctTo, currentPage+i-currint, pageSize, currentPage+i-currint);

                        }
                    }
                    output.Append(" ");
                }
                if (currentPage<totalPages)
                {
                    //处理下一页链接
                    output.AppendFormat("<a class=‘pageLink‘ href=‘{0}?pageIndex={1}&pageSize={2}‘>下一页</a>", redierctTo, currentPage+1,
                        pageSize);
                }
                else
                {
                    //为啥都注释了?
                    output.AppendFormat("<span class=‘pageLink‘>下一页</span>");
                }
                output.Append(" ");

            }
            output.AppendFormat("第{0}页/第{1}页",currentPage,totalPages);//加不加这个统计工具都行
            return new HtmlString(output.ToString());

        }//分页样式代码搞定
    }
}

 

分页“真分页”获取数据

标签:htmlhelp   pen   for   text   html   static   span   web   class   

原文地址:http://www.cnblogs.com/gdsblog/p/6512112.html

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