自己用的一个ASP.Net MVC分页拿出来分享下(转)

实例懒得做。切几个图把代码发上要用的自己搞啦~

下面是一个helper类。

namespace System.Web.Mvc
{
public enum BarStyle
{
yahoo, digg, meneame, flickr, sabrosus, scott, quotes, black, black2, grayr, yellow, jogger, starcraft2, tres, megas512, technorati, youtube, msdn, badoo, viciao, yahoo2, green_black
}
public static class PagerBarExtension
{ public static string RenderPagerBar(this HtmlHelper html, int page, int total)
{
return RenderPagerBar(html, page, total, BarStyle.technorati);
} public static string RenderPagerBar(this HtmlHelper html, int page, int total, BarStyle style)
{
return RenderPagerBar(html, page, total, style, total);
} public static string RenderPagerBar(this HtmlHelper html, int page, int total, BarStyle style, int show)
{
if (total == )
{
return "";
}
else
{
StringBuilder sb = new StringBuilder();
string _path = html.ViewContext.HttpContext.Request.Path;
sb.Append("<div class=\"");
sb.Append(style.ToString());
sb.Append("\" >"); string queryString = html.ViewContext.HttpContext.Request.QueryString.ToString();
if (queryString.IndexOf("page=") < )
{
queryString += "&page=" + page;
}
Regex re = new Regex(@"page=\d+", RegexOptions.IgnoreCase);
string result = re.Replace(queryString, "page={0}"); if (page != )
{
sb.AppendFormat("<span><a href=\"{0}\" title=\"第一页\">{1}</a></span>", _path + "?" + string.Format(result, ), "<<");
sb.AppendFormat("<span><a href=\"{0}\" title=\"上一页\">{1}</a></span>", _path + "?" + string.Format(result, page - ), "<");
}
if(page>(show+))
{
sb.AppendFormat("<span><a href=\"{0}\" title=\"前" + (show + ) + "页\">{1}</a></span>", _path + "?" + string.Format(result,page-(show + )), ".."); }
for (int i = page-show; i <= page+show; i++)
{
if (i == page)
{
sb.AppendFormat("<span class=\"current\">{0}</span>", i);
}
else
{
if (i > & i<=total)
{
sb.AppendFormat("<span><a href=\"{0}\">{1}</a></span>", _path + "?" + string.Format(result, i), i);
}
}
}
if (page < (total-(show)))
{
sb.AppendFormat("<span><a href=\"{0}\" title=\"后" + (show + ) + "页\">{1}</a></span>", _path + "?" + string.Format(result, page + (show + )), ".."); }
if (page < total)
{
sb.AppendFormat("<span><a href=\"{0}\" title=\"下一页\">{1}</a></span>", _path + "?" + string.Format(result, page + ), ">");
sb.AppendFormat("<span><a href=\"{0}\" title=\"最后一页\">{1}</a></span>", _path + "?" + string.Format(result, total), ">>"); }
sb.AppendFormat("<span class=\"current\">共{1}页</span>", page, total);
sb.Append("</div>");
return sb.ToString();
}
}
}
}

使用(VIEW):

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="indexHead" ContentPlaceHolderID="head" runat="server">
<title>Home Page</title>
<link href="/Content/pagecss.css" rel="stylesheet" type="text/css" />
</asp:Content> <asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
<%= Html.ActionLink("带其它参数","Index",new {s = "MVC"} )%>
<%= Html.ActionLink("带其它参数2","Index",new { cid = } )%>
<%= Html.ActionLink("带其它参数3","Index",new {s = "MVC" , cid = } )%> <%= Html.RenderPagerBar(Convert.ToInt32(ViewData["Page"]),Convert.ToInt32(ViewData["Total"]))%>
<%= Html.RenderPagerBar(Convert.ToInt32(ViewData["Page"]),Convert.ToInt32(ViewData["Total"]),BarStyle.badoo )%>
<%= Html.RenderPagerBar(Convert.ToInt32(ViewData["Page"]),Convert.ToInt32(ViewData["Total"]),BarStyle.badoo, )%>
<%= Html.RenderPagerBar(Convert.ToInt32(ViewData["Page"]),Convert.ToInt32(ViewData["Total"]),BarStyle.black, )%>
<%= Html.RenderPagerBar(Convert.ToInt32(ViewData["Page"]),Convert.ToInt32(ViewData["Total"]),BarStyle.digg, )%>
<%= Html.RenderPagerBar(Convert.ToInt32(ViewData["Page"]),Convert.ToInt32(ViewData["Total"]),BarStyle.flickr, )%>
<%= Html.RenderPagerBar(Convert.ToInt32(ViewData["Page"]),Convert.ToInt32(ViewData["Total"]),BarStyle.grayr, )%> </asp:Content>

最后一个int参数表示显示当前页左右各多少个页码

效果:自己用的一个ASP.Net MVC分页拿出来分享下(转)

controller:

 public ActionResult Index(int? page,string s,int? cid)
{
int _page = page??;
ViewData["Message"] = "Welcome to ASP.NET MVC!";
ViewData["Page"] = _page;
ViewData["Total"] = ; return View();
}

不是啥高深的东西,不过蛮实用,

计算总页数和skip啥的就自己搞啦。给宝宝换尿布了

实例下载:/Files/francis67/MvcPagerDemo.rar

环境:VS2008SP1 ,asp.net MVC RC1

上一篇:每日踩坑 2018-11-26 MVC Razor ActionLink 生成的URL中多生成了一个参数 ?length=n


下一篇:layui(一)——layDate组件常见用法