/// <summary> /// 分页函数 /// </summary> /// <param name="rowsCount">总记录数</param> /// <param name="pageSize">每页记录数</param> /// <param name="currentPage">当前页数</param> /// <param name="url">Url参数</param> /// <returns></returns> public string Pagination(int rowsCount, int pageSize, int currentPage, string url) { int allCurrentPage = 0; if (currentPage < 1) { currentPage = 1; } //计算总页数 if (pageSize != 0) { allCurrentPage = (rowsCount / pageSize); allCurrentPage = ((rowsCount % pageSize) != 0 ? allCurrentPage + 1 : allCurrentPage); allCurrentPage = (allCurrentPage == 0 ? 1 : allCurrentPage); } int next = currentPage + 1; int pre = currentPage - 1; int startCount = (currentPage + 5) > allCurrentPage ? allCurrentPage - 9 : currentPage - 4; //中间页终止序号 int endcount = currentPage < 5 ? 10 : currentPage + 5; //为了避免输出的时候产生负数,设置如果小于1就从序号1开始 if (startCount < 1) { startCount = 1; } //页码+5的可能性就会产生最终输出序号大于总页码,那么就要将其控制在页码数之内 if (allCurrentPage < endcount) { endcount = allCurrentPage; } string currentPageStr = "共" + allCurrentPage + "页 "; currentPageStr += currentPage > 1 ? "<a href="" + url + "?page=1">首页</a> <a href="" + url + "?page=" + pre + "">上一页</a>" : "首页 上一页"; //中间页处理,这个增加时间复杂度,减小空间复杂度 for (int i = startCount; i <= endcount; i++) { currentPageStr += currentPage == i ? " <font color="#ff0000">" + i + "</font>" : " <a href="" + url + "?page=" + i + "">" + i + "</a>"; } currentPageStr += currentPage != allCurrentPage ? " <a href="" + url + "?page=" + next + "">下一页</a> <a href="" + url + "?page=" + allCurrentPage + "">末页</a>" : " 下一页 末页"; return currentPageStr; }