首页 > 代码库 > 分页“真分页”获取数据
分页“真分页”获取数据
对于初学的新手来说,做数据分页的时候,经常是去网上下载个控件,所谓控件分页,有的人认为很好,因为方便,但是对于老鸟来说
控件分页查询效率太低,在实际工作中,也会带来致命的弱点,所以,我们一般采用真分页,部分查询,效率没得说!!
石家庄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=http://www.mamicode.com/‘{0}?pageIndex=1&pageSize={1}‘>首页", redierctTo,
pageSize);
}
if (currentPage>1)
{
//处理上一页链接
output.AppendFormat("<a class=‘pageLink‘ href=http://www.mamicode.com/‘{0}?pageIndex={1}&pageSize={2}‘>上一页", 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=http://www.mamicode.com/‘{0}?pageIndex={1}&pageSize={2}‘>{3}",redierctTo,currentPage,pageSize,currentPage);
}
else
{
//一般页处理
output.AppendFormat("<a class=‘pageLink‘ href=http://www.mamicode.com/‘{0}?pageIndex={1}&pageSize={2}‘>{3}", redierctTo, currentPage+i-currint, pageSize, currentPage+i-currint);
}
}
output.Append(" ");
}
if (currentPage<totalPages)
{
//处理下一页链接
output.AppendFormat("<a class=‘pageLink‘ href=http://www.mamicode.com/‘{0}?pageIndex={1}&pageSize={2}‘>下一页", 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=http://www.mamicode.com/‘{0}?pageIndex=1&pageSize={1}‘>首页", redierctTo,
pageSize);
}
if (currentPage>1)
{
//处理上一页链接
output.AppendFormat("<a class=‘pageLink‘ href=http://www.mamicode.com/‘{0}?pageIndex={1}&pageSize={2}‘>上一页", 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=http://www.mamicode.com/‘{0}?pageIndex={1}&pageSize={2}‘>{3}",redierctTo,currentPage,pageSize,currentPage);
}
else
{
//一般页处理
output.AppendFormat("<a class=‘pageLink‘ href=http://www.mamicode.com/‘{0}?pageIndex={1}&pageSize={2}‘>{3}", redierctTo, currentPage+i-currint, pageSize, currentPage+i-currint);
}
}
output.Append(" ");
}
if (currentPage<totalPages)
{
//处理下一页链接
output.AppendFormat("<a class=‘pageLink‘ href=http://www.mamicode.com/‘{0}?pageIndex={1}&pageSize={2}‘>下一页", redierctTo, currentPage+1,
pageSize);
}
else
{
//为啥都注释了?
output.AppendFormat("<span class=‘pageLink‘>下一页</span>");
}
output.Append(" ");
}
output.AppendFormat("第{0}页/第{1}页",currentPage,totalPages);//加不加这个统计工具都行
return new HtmlString(output.ToString());
}//分页样式代码搞定
}
}
分页“真分页”获取数据