zoukankan      html  css  js  c++  java
  • 自己用的一个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 == 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 = page-show; 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

  • 相关阅读:
    1462. 课程安排 IV
    最全的CSS浏览器兼容问题【FF与IE】
    this&super两个关键字的意义和用法
    Javascript this 的一些总结
    JS-封装类或对象的最佳方案
    背景色透明,里面内容(图片、文字)不透明
    css3 前端开发
    html5游戏之Box2d物理引擎集成
    Box2d引擎之元素
    西天取经第一步——制作自己的HTML5游戏
  • 原文地址:https://www.cnblogs.com/superfeeling/p/5049790.html
Copyright © 2011-2022 走看看