MVC的一个分页,网上摘录,自己少量修改
分页类
using System.Text;
using System.Web.Routing;
namespace System.Web.Mvc
{
public static class PagerHelper
{
/// <summary>
/// 获取分页
/// </summary>
/// <param name="urlFormat">url格式,用{0}代替页码</param>
/// <param name="pageIndex">当前页</param>
/// <param name="pageSize">总页数</param>
/// <param name="recordCount">总记录数</param>
/// <param name="mode">分页模式</param>
/// <returns>html</returns>
public static string Pager(this HtmlHelper helper, string urlFormat, int pageIndex, int pageSize, int recordCount, PageMode mode)
{
urlFormat = urlFormat.Replace("%7B0%7D", "{0}");
int pageCount = (recordCount % pageSize == 0 ? recordCount / pageSize : recordCount / pageSize + 1);
StringBuilder sb = new StringBuilder();
sb.AppendFormat("总共{0}条记录,共{1}页,当前第{2}页 ", recordCount, pageCount, pageIndex);
if (pageIndex == 1)
sb.Append("首页 ");
else
{
string url1 = string.Format(urlFormat.ToString(), 1);
sb.AppendFormat("<a href={0}>首页</a> ", url1);
}
if (pageIndex > 1)
{
string url1 = string.Format(urlFormat.ToString(), pageIndex - 1);
sb.AppendFormat("<a href={0}>上一页</a> ", url1);
}
else
sb.Append("上一页 ");
if (mode == PageMode.Numeric)
sb.Append(GetNumericPage(urlFormat.ToString(), pageIndex, pageSize, recordCount, pageCount));
if (pageIndex < pageCount)
{
string url1 = string.Format(urlFormat.ToString(), pageIndex + 1);
sb.AppendFormat("<a href={0}>下一页</a> ", url1);
}
else
sb.Append("下一页 ");
if (pageIndex == pageCount)
sb.Append("末页");
else
{
string url1 = string.Format(urlFormat.ToString(), pageCount);
sb.AppendFormat("<a href={0}>末页</a>", url1);
}
return sb.ToString();
}
/// <summary>
/// 获取数字分页
/// </summary>
private static string GetNumericPage(string url, int pageIndex, int pageSize, int recordCount, int pageCount)
{
int k = pageIndex / 10;
int m = pageIndex % 10;
StringBuilder sb = new StringBuilder();
if (pageIndex / 10 == pageCount / 10)
{
if (m == 0)
{
k--;
m = 10;
}
else
m = pageCount % 10;
}
else
m = 10;
for (int i = k * 10 + 1; i <= k * 10 + m; i++)
{
if (i == pageIndex)
sb.AppendFormat("<span style=\"color: red\">{0}</span> ", i);
else
{
string url1 = string.Format(url.ToString(), i);
sb.AppendFormat("<a href={0}>{1}</a> ", url1, i);
}
}
return sb.ToString();
}
}
/// <summary>
/// 分页模式
/// </summary>
public enum PageMode
{
/// <summary>
/// 普通分页模式
/// </summary>
Normal,
/// <summary>
/// 普通分页加数字分页
/// </summary>
Numeric
}
}
using System.Web.Routing;
namespace System.Web.Mvc
{
public static class PagerHelper
{
/// <summary>
/// 获取分页
/// </summary>
/// <param name="urlFormat">url格式,用{0}代替页码</param>
/// <param name="pageIndex">当前页</param>
/// <param name="pageSize">总页数</param>
/// <param name="recordCount">总记录数</param>
/// <param name="mode">分页模式</param>
/// <returns>html</returns>
public static string Pager(this HtmlHelper helper, string urlFormat, int pageIndex, int pageSize, int recordCount, PageMode mode)
{
urlFormat = urlFormat.Replace("%7B0%7D", "{0}");
int pageCount = (recordCount % pageSize == 0 ? recordCount / pageSize : recordCount / pageSize + 1);
StringBuilder sb = new StringBuilder();
sb.AppendFormat("总共{0}条记录,共{1}页,当前第{2}页 ", recordCount, pageCount, pageIndex);
if (pageIndex == 1)
sb.Append("首页 ");
else
{
string url1 = string.Format(urlFormat.ToString(), 1);
sb.AppendFormat("<a href={0}>首页</a> ", url1);
}
if (pageIndex > 1)
{
string url1 = string.Format(urlFormat.ToString(), pageIndex - 1);
sb.AppendFormat("<a href={0}>上一页</a> ", url1);
}
else
sb.Append("上一页 ");
if (mode == PageMode.Numeric)
sb.Append(GetNumericPage(urlFormat.ToString(), pageIndex, pageSize, recordCount, pageCount));
if (pageIndex < pageCount)
{
string url1 = string.Format(urlFormat.ToString(), pageIndex + 1);
sb.AppendFormat("<a href={0}>下一页</a> ", url1);
}
else
sb.Append("下一页 ");
if (pageIndex == pageCount)
sb.Append("末页");
else
{
string url1 = string.Format(urlFormat.ToString(), pageCount);
sb.AppendFormat("<a href={0}>末页</a>", url1);
}
return sb.ToString();
}
/// <summary>
/// 获取数字分页
/// </summary>
private static string GetNumericPage(string url, int pageIndex, int pageSize, int recordCount, int pageCount)
{
int k = pageIndex / 10;
int m = pageIndex % 10;
StringBuilder sb = new StringBuilder();
if (pageIndex / 10 == pageCount / 10)
{
if (m == 0)
{
k--;
m = 10;
}
else
m = pageCount % 10;
}
else
m = 10;
for (int i = k * 10 + 1; i <= k * 10 + m; i++)
{
if (i == pageIndex)
sb.AppendFormat("<span style=\"color: red\">{0}</span> ", i);
else
{
string url1 = string.Format(url.ToString(), i);
sb.AppendFormat("<a href={0}>{1}</a> ", url1, i);
}
}
return sb.ToString();
}
}
/// <summary>
/// 分页模式
/// </summary>
public enum PageMode
{
/// <summary>
/// 普通分页模式
/// </summary>
Normal,
/// <summary>
/// 普通分页加数字分页
/// </summary>
Numeric
}
}
<%= Html.Pager(Url.Action("Main",new {page="{0}"}),10,10,1000, PageMode.Numeric) %>
以{0}代替页索引的参数即可,使用非常方便.