---------------分页方法-----------------
public static class PagerHelper
{
/// <summary>
/// 分页
/// </summary>
/// <param name="helper"></param>
/// <param name="id">分页id</param>
/// <param name="currentPageIndex">当前页</param>
/// <param name="pageSize">分页尺寸</param>
/// <param name="recordCount">记录总数</param>
/// <param name="htmlAttributes">分页头标签属性</param>
/// <param name="className">分页样式</param>
/// <param name="mode">分页模式</param>
/// <returns></returns>
public static string Pager(this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount, object htmlAttributes, string className,PageMode mode)
{
TagBuilder builder = new TagBuilder("table");
builder.IdAttributeDotReplacement = "_";
builder.GenerateId(id);
builder.AddCssClass(className);
builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
builder.InnerHtml = GetNormalPage(currentPageIndex, pageSize, recordCount,mode);
return builder.ToString();
}
/// <summary>
/// 分页
/// </summary>
/// <param name="helper"></param>
/// <param name="id">分页id</param>
/// <param name="currentPageIndex">当前页</param>
/// <param name="pageSize">分页尺寸</param>
/// <param name="recordCount">记录总数</param>
/// <param name="className">分页样式</param>
/// <returns></returns>
public static string Pager(this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount, string className)
{
return Pager(helper, id, currentPageIndex, pageSize, recordCount, null, className,PageMode.Normal);
}
/// <summary>
/// 分页
/// </summary>
/// <param name="helper"></param>
/// <param name="id">分页id</param>
/// <param name="currentPageIndex">当前页</param>
/// <param name="pageSize">分页尺寸</param>
/// <param name="recordCount">记录总数</param>
/// <returns></returns>
public static string Pager(this HtmlHelper helper,string id,int currentPageIndex,int pageSize,int recordCount)
{
return Pager(helper, id, currentPageIndex, pageSize, recordCount,null);
}
/// <summary>
/// 分页
/// </summary>
/// <param name="helper"></param>
/// <param name="id">分页id</param>
/// <param name="currentPageIndex">当前页</param>
/// <param name="pageSize">分页尺寸</param>
/// <param name="recordCount">记录总数</param>
/// <param name="mode">分页模式</param>
/// <returns></returns>
public static string Pager(this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount,PageMode mode)
{
return Pager(helper, id, currentPageIndex, pageSize, recordCount, null,mode);
}
/// <summary>
/// 分页
/// </summary>
/// <param name="helper"></param>
/// <param name="id">分页id</param>
/// <param name="currentPageIndex">当前页</param>
/// <param name="pageSize">分页尺寸</param>
/// <param name="recordCount">记录总数</param>
/// <param name="className">分页样式</param>
/// <param name="mode">分页模式</param>
/// <returns></returns>
public static string Pager(this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount,string className, PageMode mode)
{
return Pager(helper, id, currentPageIndex, pageSize, recordCount, null,className,mode);
}
/// <summary>
/// 获取普通分页
/// </summary>
/// <param name="currentPageIndex"></param>
/// <param name="pageSize"></param>
/// <param name="recordCount"></param>
/// <returns></returns>
public static string GetNormalPage(int currentPageIndex, int pageSize, int recordCount,PageMode mode)
{
int pageCount = (recordCount%pageSize ==0?recordCount/pageSize:recordCount/pageSize+1);
StringBuilder url = new StringBuilder();
url.Append(HttpContext.Current.Request.Url.AbsolutePath+"?page={0}");
NameValueCollection collection = HttpContext.Current.Request.QueryString;
string[] keys = collection.AllKeys;
for (int i = 0; i < keys.Length; i++)
{
if (keys[i].ToLower() != "page")
url.AppendFormat("&{0}={1}", keys[i], collection[keys[i]]);
}
StringBuilder sb = new StringBuilder();
sb.Append("<tr><td>");
sb.AppendFormat("总共{0}条记录,共{1}页,当前第{2}页 ", recordCount, pageCount, currentPageIndex);
if (currentPageIndex == 1)
sb.Append("<span>首页</span> ");
else
{
string url1 = string.Format(url.ToString(), 1);
sb.AppendFormat("<span><a href={0}>首页</a></span> ", url1);
}
if (currentPageIndex > 1)
{
string url1 = string.Format(url.ToString(), currentPageIndex - 1);
sb.AppendFormat("<span><a href={0}>上一页</a></span> ", url1);
}
else
sb.Append("<span>上一页</span> ");
if(mode == PageMode.Numeric)
sb.Append(GetNumericPage(currentPageIndex,pageSize,recordCount,pageCount,url.ToString()));
if (currentPageIndex < pageCount)
{
string url1 = string.Format(url.ToString(), currentPageIndex+1);
sb.AppendFormat("<span><a href={0}>下一页</a></span> ", url1);
}
else
sb.Append("<span>下一页</span> ");
if (currentPageIndex == pageCount)
sb.Append("<span>末页</span> ");
else
{
string url1 = string.Format(url.ToString(), pageCount);
sb.AppendFormat("<span><a href={0}>末页</a></span> ", url1);
}
return sb.ToString();
}
/// <summary>
/// 获取数字分页
/// </summary>
/// <param name="currentPageIndex"></param>
/// <param name="pageSize"></param>
/// <param name="recordCount"></param>
/// <param name="pageCount"></param>
/// <param name="url"></param>
/// <returns></returns>
private static string GetNumericPage(int currentPageIndex, int pageSize, int recordCount, int pageCount,string url)
{
int k = currentPageIndex / 10;
int m = currentPageIndex % 10;
StringBuilder sb = new StringBuilder();
if (currentPageIndex / 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 == currentPageIndex)
sb.AppendFormat("<span><font color=red><b>{0}</b></font></span> ", i);
else
{
string url1 = string.Format(url.ToString(), i);
sb.AppendFormat("<span><a href={0}>{1}</a></span> ",url1, i);
}
}
return sb.ToString();
}
}
/// <summary>
/// 分页模式
/// </summary>
public enum PageMode
{
/// <summary>
/// 普通分页模式
/// </summary>
Normal,
/// <summary>
/// 普通分页加数字分页
/// </summary>
Numeric
}
----------------------------控制器调用----------------------------------------------
public ActionResult Index()
{
//多个动作可以使用隐藏控件值判断
int pageIndex = GetRequestInt("page") < 1 ? 1 : GetRequestInt("page");
int pageSize = GetRequestInt("pagesize") < 1 ? 10 : GetRequestInt("pagesize");
//string key = GetRequestString("key").Replace(",", "");//关键字
//string checkstatus = GetRequestString("checkstatus");//审核状态
////检查是否有删除
//string DelIdString = GetRequestString("cbSelected");
//if (!string.IsNullOrEmpty(DelIdString))
//{
// foreach (var item in DelIdString.Split(‘,‘))
// {
// string _id = item.Replace("‘", "");
// db.Delete("dt_Job", "Id", null, _id);
// }
//}
//int count = 0;
//string _where = "1=1";
//if (checkstatus.Equals("0") || checkstatus.Equals("1"))
//{
// _where += " and CheckStatus=" + checkstatus;
//}
//if (!string.IsNullOrEmpty(key))
//{
// _where += string.Format(" and ( Name like ‘%{0}%‘ or ApplyOrganization like ‘%{0}%‘ )", key);
//}
//var temp = db.Page<Job>(pageIndex, pageSize, "select * from dt_Job where " + _where + " Order by CreateTime desc,id");
var temp = db.Page<Job>(pageIndex, pageSize, "select * from dt_Job Order by CreateTime desc,id");
//返回分页导航和列表
ListInfo Result = new ListInfo()
{
PageItemInfo = temp,
//key = key,
//checkstatus = checkstatus,
PageString = PagerHelper.GetNormalPage(pageIndex, pageSize, (int)temp.TotalItems, PageMode.Normal)
};
return View(Result);
}
-------------------------------------------------------------html页面显示-----------------------------------------------------------------------
<form method="get" id="flist" name="flist">
@*<div id="content">
<div class="tools_box">
<div class="tools_bar">
<div class="search_box">
<input name="key" type="text" id="txtKeywords" class="txtInput" value="@Model.key">
<input type="submit" name="submit" value="搜索" id="submit" class="btnSearch">
</div>
<a href="javascript:void(0);" onclick="checkAll(this);" class="tools_btn"><span><b class="all">全选</b></span></a>
@* <a id="LinkButton2" class="tools_btn" href="javascript:__doPostBack(‘LinkButton2‘,‘‘)"><span><b class="send">全部审核</b></span></a>
<a onclick="return delJob(‘btnDelete‘);" id="btnDelete" class="tools_btn" href="javascript:void(0)"><span><b class="delete">批量删除</b></span></a>
</div>
<div class="select_box">
请选择:
<select name="checkstatus" onchange="tijiao()" id="checkstatus" class="select2">
<option value="">所有属性</option>
<option value="1" @{if (@Model.checkstatus=="1"){string res="selected";@res }}>已审核</option>
<option value="0" @{if (@Model.checkstatus=="0"){string res="selected";@res }}>未审核</option>
</select>
</div>
</div>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="msgtable">
<tbody>
<tr class="tr_odd_bg">
<th width="6%">选择</th>
<th width="150" align="left">姓名</th>
<th align="left">报考单位</th>
<th align="left">岗位及代码</th>
<th width="160">申请时间</th>
<th width="120">审核状态</th>
<th width="100">浏览</th>
</tr>
@foreach (var item in Model.PageItemInfo.Items)
{
<tr class="">
<td align="center" class="checkall"> <input type="checkbox" name="cbSelected" value="@item.Id" /> </td>
<td>@item.Name</td>
<td>@item.ApplyOrganization</td>
<td>@item.ApplyCode</td>
<td align="center">@item.CreateTime.ToString("yyyy-MM-dd HH:mm:ss")</td>
<td align="center">
@{if (item.CheckStatus == 1)
{
string res = "已审核";
@res
}
else
{
string res = "<font color=‘red‘>未审核</font>";
@Html.Raw(res)
}
}
</td>
<td align="center">@Html.ActionLink("查看", "Detail", new { id = item.Id }, new { target = "_blank" }) </td>
</tr>
}
</tbody>
</table>
<div class="line15"></div>*@
<div class="page_box">
<div id="PageContent" class="flickr right">@Html.Raw(Model.PageString)</div>
<div class="left">
显示<input name="pagesize" type="text" value="@Model.PageItemInfo.ItemsPerPage" onchange="tijiao()" id="pagesize" class="txtInput2 small2">条/页
</div>
</div>
</div>
</form>