分页

<div>
@{
var pageTotal = Convert.ToInt32(Request["pageTotal"]);
var toolName = "tool.Paging";
if (ViewData["toolName"] != null)
{
toolName = ViewData["toolName"] + "";
}
if (ViewData["pageTotal"] != null)
{
pageTotal = Convert.ToInt32(ViewData["pageTotal"]);
}
var currentPageIndex = Convert.ToInt32(Request["currentPageIndex"]);

if (pageTotal <= 1)
{
pageTotal = 1;
}
if (currentPageIndex <= 1)
{
currentPageIndex = 1;
}
if (currentPageIndex >= pageTotal)
{
currentPageIndex = pageTotal;
}
var startPage = currentPageIndex - 2 >= 1 ? currentPageIndex - 2 : 1;
var endPage = currentPageIndex + 2 <= pageTotal ? currentPageIndex + 2 : pageTotal;
if (currentPageIndex + 2 > pageTotal) { startPage = pageTotal - 4 >= 1 ? pageTotal - 4 : 1; }
if (currentPageIndex - 2 <= 1) { endPage = startPage + 4 >= pageTotal ? pageTotal : startPage + 4; }

var nextPageIndex = currentPageIndex >= pageTotal ? currentPageIndex : currentPageIndex + 1;
<ul class="pagination pull-right" style="margin: 0; font-size: 12px;">
<li><a href="javascript:@(toolName)(1)">首页</a></li>
<li><a href="javascript:@(toolName)(@(currentPageIndex - 1 < 1 ? 1 : currentPageIndex - 1))">上一页</a></li>
@for (var i = startPage; i <= endPage; i++)
{
<li class="@(currentPageIndex == i ? "active" : "")"><a href="javascript:@(toolName)(@(i))">@(i)</a></li>
}
<li><a href="javascript:@toolName (@nextPageIndex)">下一页</a></li>
<li><a href="javascript:@toolName (@pageTotal)">尾页</a></li>
<li class="disabled"><a href="javascript:void(0)">第@(currentPageIndex)/@(pageTotal)页</a></li>
</ul>
}
<div style="clear: both;"></div>
</div>

原文地址:https://www.cnblogs.com/jooucks/p/7048592.html