熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

自己用的一個MVC Pager

2013-11-13 09:58:44  來源: .NET編程 

  實例懶得做切幾個圖把代碼發上要用的自己搞啦~

  下面是一個helper類

  Code
namespace SystemWebMvc
{
    public enum BarStyle
    {
        yahoo digg meneame flickr sabrosus scott quotes black black grayr yellow jogger starcraft tres megas technorati youtube msdn badoo viciao yahoo green_black
    }
    public static class PagerBarExtension
    {

  public static string RenderPagerBar(this HtmlHelper html int page int total)
        {
            return RenderPagerBar(html page total BarStyletechnorati);
        }

  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 = htmlViewContextHttpContextRequestPath;
                sbAppend(<div class=\);
                sbAppend(styleToString());
                sbAppend(\ >);

  string queryString = htmlViewContextHttpContextRequestQueryStringToString();
                if (queryStringIndexOf(page=) < )
                {
                    queryString += &page= + page;
                }
                Regex re = new Regex(@page=\d+ RegexOptionsIgnoreCase);
                string result = reReplace(queryString page={});

  if (page != )
                {
                    sbAppendFormat(<span><a href=\{}\ title=\第一頁\>{}</a></span> _path + ? + stringFormat(result ) <<);
                    sbAppendFormat(<span><a href=\{}\ title=\上一頁\>{}</a></span> _path + ? + stringFormat(result page ) <);
                }
                if(page>(show+))
                {
                    sbAppendFormat(<span><a href=\{}\ title=\ + (show + ) + 頁\>{}</a></span> _path + ? + stringFormat(resultpage(show + )) );

  }
                for (int i = pageshow; i <= page+show; i++)
                {
                    if (i == page)
                    {
                        sbAppendFormat(<span class=\current\>{}</span> i);
                    }
                    else
                    {
                        if (i > & i<=total)
                        {
                            sbAppendFormat(<span><a href=\{}\>{}</a></span> _path + ? + stringFormat(result i) i);
                        }
                    }
                }
                if (page < (total(show)))
                {
                    sbAppendFormat(<span><a href=\{}\ title=\ + (show + ) + 頁\>{}</a></span> _path + ? + stringFormat(result page + (show + )) );

  }
                if (page < total)
                {
                    sbAppendFormat(<span><a href=\{}\ title=\下一頁\>{}</a></span> _path + ? + stringFormat(result page + ) >);
                    sbAppendFormat(<span><a href=\{}\ title=\最後一頁\>{}</a></span> _path + ? + stringFormat(result total) >>);

  }
                sbAppendFormat(<span class=\current\>共{}頁</span> page total);
                sbAppend(</div>);
                return sbToString();
            }
        }
    }
}
 

  使用(VIEW)

  Code
<%@ Page Language=C# MasterPageFile=~/Views/Shared/SiteMaster Inherits=SystemWebMvcViewPage %>

  <asp:Content ID=indexHead ContentPlaceHolderID=head runat=server>
    <title>Home Page</title>
    <link rel=stylesheet type=text/css />
</asp:Content>
   
<asp:Content ID=indexContent ContentPlaceHolderID=MainContent runat=server>
    <%= HtmlActionLink(帶其它參數Indexnew {s = MVC} )%>
    <%= HtmlActionLink(帶其它參數Indexnew { cid = } )%>
    <%= HtmlActionLink(帶其它參

  數Indexnew {s = MVC cid = } )%>
   
    <%= HtmlRenderPagerBar(ConvertToInt(ViewData[Page])ConvertToInt(ViewData[Total]))%>
    <%= HtmlRenderPagerBar(ConvertToInt(ViewData[Page])ConvertToInt(ViewData[Total])BarStylebadoo )%>
    <%= HtmlRenderPagerBar(ConvertToInt(ViewData[Page])ConvertToInt(ViewData[Total])BarStylebadoo )%>
    <%= HtmlRenderPagerBar(ConvertToInt(ViewData[Page])ConvertToInt(ViewData[Total])BarStyleblack )%>
    <%= HtmlRenderPagerBar(ConvertToInt(ViewData[Page])ConvertToInt(ViewData[Total])BarStyledigg )%>
    <%= HtmlRenderPagerBar(ConvertToInt(ViewData[Page])ConvertToInt(ViewData[Total])BarStyleflickr )%>
    <%= HtmlRenderPagerBar(ConvertToInt(ViewData[Page])ConvertToInt(ViewData[Total])BarStylegrayr )%>
   
</asp:Content>
最後一個int參數表示顯示當前頁左右各多少個頁碼

  效果

   public ActionResult Index(int? pagestring sint? cid)
        {
            int _page = page??;
            ViewData[Message] = Welcome to ASPNET MVC!;
            ViewData[Page] = _page;
            ViewData[Total] = ;

  return View();
        }
不是啥高深的東西不過蠻實用

  計算總頁數和skip啥的就自己搞啦


From:http://tw.wingwit.com/Article/program/net/201311/12180.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.