[C#]Asp.net MVC:自定义的MVC Pager

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

下面是一个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 == 1)
            
{
                
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="< 0)
                
{
                    queryString 
+= "&page=" + page;
                }

                Regex re 
= new Regex(@"page=\d+", RegexOptions.IgnoreCase);
                
string result = re.Replace(queryString, "page={0}");

                
if (page != 1)
                
{
                    sb.AppendFormat(
"<span><a href=\"{0}\" title=\"第一页\">{1}</a></span>", _path + "?" + string.Format(result, 1), "<<");
                    sb.AppendFormat(
"<span><a href=\"{0}\" title=\"上一页\">{1}</a></span>", _path + "?" + string.Format(result, page  1), "<");
                }

                
if(page>(show+1))
                
{
                    sb.AppendFormat(
"<span><a href=\"{0}\" title=\"" + (show + 1) + "页\">{1}</a></span>", _path + "?" + string.Format(result,page(show + 1)), "..");

                }

                
for (int i = pageshow; i <= page+show; i++)
                
{
                    
if (i == page)
                    
{
                        sb.AppendFormat(
"<span class=\"current\">{0}</span>", i);
                    }

                    
else
                    
{
                        
if (i > 0 & 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) + "页\">{1}</a></span>", _path + "?" + string.Format(result, page + (show + 1)), "..");

                }

                
if (page < total)
                
{
                    sb.AppendFormat(
"<span><a href=\"{0}\" title=\"下一页\">{1}</a></span>", _path + "?" + string.Format(result, page + 1), ">");
                    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 = 3} )%>
    
<%= Html.ActionLink("带其它参数3","Index",new {s = "MVC" , cid = 5} )%>
    
    
<%= 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,3 )%>
    
<%= Html.RenderPagerBar(Convert.ToInt32(ViewData["Page"]),Convert.ToInt32(ViewData["Total"]),BarStyle.black,4 )%>
    
<%= Html.RenderPagerBar(Convert.ToInt32(ViewData["Page"]),Convert.ToInt32(ViewData["Total"]),BarStyle.digg,5 )%>
    
<%= Html.RenderPagerBar(Convert.ToInt32(ViewData["Page"]),Convert.ToInt32(ViewData["Total"]),BarStyle.flickr,3 )%>
    
<%= Html.RenderPagerBar(Convert.ToInt32(ViewData["Page"]),Convert.ToInt32(ViewData["Total"]),BarStyle.grayr,3 )%>
    
</asp:Content>

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

 效果:

 

controller:

 


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

            return View();
        }

 

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

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

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

环境:VS2008SP1 ,ASP.NET MVC RC1

赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏