C#实现分页的算法

C#实现分页的算法

1 新建DataPages分页数据类如下

public class DataPages<T> where T : class, new()
{
/// <summary>
/// 页码
/// </summary>
public int PageIndex { get; set; }

//每页大小
public int PageSize { get; set; }

/// <summary>
/// 集合数据
/// </summary>
public IList<T> Records { get; set; } = new List<T>();

/// <summary>
/// 总记录数
/// </summary>
public int TotalRecords { get; set; }

/// <summary>
/// 当前点击的样式
/// </summary>
public string CurrentLinkClassName { get; set; }

public DataPages()
{
this.PageIndex = 1;
this.PageSize = 10;
}

/// <summary>
/// 分页数据
/// </summary>
/// <param name="helper"></param>
/// <param name="ShowPage">每屏显示多少页码 默认每屏10条</param>
/// <param name="CurrentLinkClassName">每当前点击的样式</param>
/// <returns></returns>
public string ManageRenderPager(System.Web.Mvc.UrlHelper helper, int ShowPage,string CurrentLinkClassName)
{
return PageHelper.ManagePager(helper, TotalRecords, PageSize, PageIndex, ShowPage,CurrentLinkClassName);
}
}

2实现分页的算法

/// <summary>
/// 通用分页
/// </summary>
public static class PageHelper
{
/// <summary>
/// 通用分页
/// </summary>
/// <param name="helper"></param>
/// <param name="TotalRecords">记录数</param>
/// <param name="PageSize">页数</param>
/// <param name="PageIndex">页码</param>
/// <param name="ShowPage">每屏显示多少页码 默认每屏10条</param>
/// <param name="CurrentLinkClassName">每当前点击的样式</param>
/// <returns></returns>
public static string ManagePager(System.Web.Mvc.UrlHelper helper, int TotalRecords, int PageSize, int PageIndex,int ShowPage,string CurrentLinkClassName)
{
//构建查询字符串
string pageQuery = HttpContext.Current.Request.Url.AbsolutePath + "?";

//获取Url请求的参数组合Url信息
foreach (string item in HttpContext.Current.Request.QueryString.Keys)
{
if (!string.IsNullOrEmpty(item)&& item.ToUpper()=="PAGE")
{
continue;
}
pageQuery+=(item+"="+HttpUtility.UrlEncode(HttpContext.Current.Request.QueryString[item]))+"&";
}

StringBuilder stringBuilder = new StringBuilder();

//显示的分页数量
int pageCount = TotalRecords % PageSize > 0 ? TotalRecords / PageSize + 1 : TotalRecords / PageSize;

if (PageIndex < 1)
{
PageIndex = 1;
}
else if (PageIndex > pageCount)
{
PageIndex = pageCount;
}

//上一页
int PrevIdx = PageIndex <= 1 ? 1 : PageIndex - 1;
//下一页
int MextIdx = PageIndex + 1 < pageCount ? PageIndex + 1 : pageCount;
//每组页码多少条
int PageLinkCount = ShowPage;

stringBuilder.AppendFormat("当前第<b style=\"font-size:14px;color:#367DA4\">{0}</b>页&nbsp;", PageIndex);
stringBuilder.Append(@"<ul class=‘pagination‘ data-currentpage=‘10‘ data-firstpage=‘ / mvcpager / demos / applycss / ‘ data-invalidpageerrmsg=‘页索引无效‘ data-outrangeerrmsg=‘页索引超出范围‘ data-pagecount=‘80‘ data-pageparameter=‘id‘ data-pagerid=‘Webdiyer.MvcPager‘ data-urlformat=‘ / mvcpager / demos / applycss / __id__ / ‘ id=‘bootstrappager‘>");
if (TotalRecords>0)
{

stringBuilder.AppendFormat("<li><a href=\"{0}\">首页</a></li>&nbsp;", pageQuery + "Page=1");
stringBuilder.AppendFormat("<li><a class=\"ch\" href=\"{0}\">上一页</a></li>&nbsp;", pageQuery + "Page=" + PrevIdx);

int index = PageIndex;
if (index > PageLinkCount / 2)
{
index = index - (PageLinkCount / 2);
}
else
{
index = 1;
}

for (int i = 0; i < PageLinkCount; i++)
{
if (index + i <= pageCount)
{
if (PageIndex == index + i)
{
stringBuilder.AppendFormat("<li><a class=\"ahover\" href=\"{0}\">{1}</a></li>&nbsp;", pageQuery + "Page=" + (index + i), index + i);
}
else
{
stringBuilder.AppendFormat("<li><a href=\"{0}\">{1}</a></li>&nbsp;", pageQuery + "Page=" + (index + i).ToString(), index + i);
}
}
}
stringBuilder.AppendFormat("<li><a class=\"ch\" href=\"{0}\">下一页</a></li>&nbsp;", pageQuery + "Page=" + MextIdx);
stringBuilder.AppendFormat("<li><a class=\"ch\" href=\"{0}\">尾页</a></li>&nbsp;", pageQuery + "Page=" + pageCount);
}

stringBuilder.Append("</ul>");
return stringBuilder.ToString();
}
}

展示效果C#实现分页的算法