Asp.net MVC 简单分页:
public static string Pager(int page,int pageSize,int total)
{
var htmlStr = "<div class=\"pagination\">";
var pageCount = Math.Ceiling((double)total / pageSize);
var request = HttpContext.Current.Request;
var url = request.RawUrl;
if (url.IndexOf("page") < 0)
{
if (url.IndexOf('?') < 0)
{
url += "?page={0}";
}
else
{
url += "&page={0}";
}
}
else
{
url = Regex.Replace(url,@"page=\d*", "page={0}");
}
if (url.IndexOf("pageSize") < 0)
{
if (url.IndexOf('?') < 0)
{
url += "?pageSize={1}";
}
else
{
url += "&pageSize={1}";
}
}
else
{
url = Regex.Replace(url, @"pageSize=\d*", "pageSize={1}");
}
var perEveryTime = 5;//每次显示可点击页数
var urlTmp = "";
var count = 0;
var paginationPreTemplate = "<span class=\"pagination-link pagination-prev\"><a href=\"{0}\">{1}</a></span>";
var paginationNextTemplate = "<span class=\"pagination-link pagination-next\"><a href=\"{0}\">{1}</a></span>";
var paginationLinkTemplate = "<span class=\"pagination-link\"><a href=\"{0}\">{1}</a></span>";
var paginationCurrentTemplate = "<span class=\"pagination-current\">{0}</span>";
var paginationDotsTemplate = "<span class=\"pagination-link\">...</span>";
if (page > 1)//前一页
{
urlTmp = string.Format(url, page - 1, pageSize);
htmlStr += string.Format(paginationPreTemplate, urlTmp, "<");
}
if (pageCount <= perEveryTime)//将所有页面展示出来
{
for (var i = 1; i <= pageCount; i++)
{
if (i == page)
{
htmlStr += string.Format(paginationCurrentTemplate, page);
}
else
{
urlTmp = string.Format(url, i, pageSize);
htmlStr += string.Format(paginationLinkTemplate, urlTmp, i);
}
}
}
else//只展示perEveryTime个页面,并采用:p1+...+p2p3p4+...p5,的形式
{
if (page - 1 <= 2)//不需要加第一个“...”
{
for (var i = 1; i <= page; i++)
{
if (i == page)
{
htmlStr += string.Format(paginationCurrentTemplate,page);
}
else
{
urlTmp = string.Format(url, i, pageSize);
htmlStr += string.Format(paginationLinkTemplate, urlTmp, i);
}
}
count += page;
}
else//加第一个“...”
{
urlTmp = string.Format(url, 1, pageSize);
htmlStr += string.Format(paginationLinkTemplate, urlTmp, 1);
htmlStr += paginationDotsTemplate;
urlTmp = string.Format(url, page-1, pageSize);
htmlStr += string.Format(paginationLinkTemplate, urlTmp, (page - 1));
htmlStr += string.Format(paginationCurrentTemplate, page);
count += 3;
}
if (pageCount-page <= 2)//不需要加第二个“...”
{
for (var i = page+1; i <= pageCount; i++)
{
if (i == page)
{
htmlStr += string.Format(paginationCurrentTemplate, page);
}
else
{
urlTmp = string.Format(url, i, pageSize);
htmlStr += string.Format(paginationLinkTemplate, urlTmp, i);
}
}
}
else
{
urlTmp = string.Format(url, page+1, pageSize);
htmlStr += string.Format(paginationLinkTemplate, urlTmp, page + 1);
count++;
htmlStr += paginationDotsTemplate;
for (var i = pageCount - (perEveryTime-count)+1; i <= pageCount; i++)
{
urlTmp = string.Format(url, i, pageSize);
htmlStr += string.Format(paginationLinkTemplate, urlTmp, i);
}
}
}
if (page < pageCount)//后一页
{
urlTmp = string.Format(url, page + 1, pageSize);
htmlStr += string.Format(paginationNextTemplate, urlTmp, ">");
}
htmlStr += "</div>";
return htmlStr;
}
调用:
后台:var t = HtmlHelper2.Pager(page, pageSize, users.Count);
ViewBag.t = t;
前台:
@Html.Raw(ViewBat.t)
<style type="text/css">
/* Pagination */
.pagination {text-align:center;padding:0 0 40px;font-family: 'ProximaNovaT-Thin';}
.pagination a,
.pagination a:link,
.pagination a:visited {font-size:20px;color:#999;height:28px;line-height:28px;width:23px;text-align:center;display:inline-block;border-radius:15px;margin:0px 2px;}
.pagination a:focus,
.pagination a:hover,
.pagination a:active {color:#3498db;}
.pagination-link {color:#bbb;}
.pagination-prevk {margin-right:10px;color:#aaa;}
.pagination-next {margin-left:10px;color:#aaa;}
.pagination-current {background:#999;color:#fff;font-family: 'ProximaNovaT-Thin';height:28px;line-height:30px;width:28px;text-align:center;display:inline-block;border-radius:15px;margin:0 2px;}
</style>