本篇內(nèi)容介紹了“ASP.NET MVC分頁控件的實(shí)現(xiàn)方法”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供富裕網(wǎng)站建設(shè)、富裕做網(wǎng)站、富裕網(wǎng)站設(shè)計(jì)、富裕網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、富裕企業(yè)網(wǎng)站模板建站服務(wù),十余年富裕做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
需求及模擬代碼
需求,假設(shè)我們有個(gè)列表,有分頁功能,我們可能需要一個(gè)頁碼列表,如
我們模擬寫一下Action:
public ActionResult Index(int? p) { if (!p.HasValue) p = 1;//如果未對(duì)p傳值就是第1頁 var list = new List< int>();//生成一個(gè)模擬列表 for (var i = 0; i < 10;i++ ) { list.Add(p.Value);//是第幾頁就向中填充幾個(gè)這個(gè)頁碼的數(shù) } return View(list);//強(qiáng)型傳遞給View
View中我寫以下顯示方式:
< %@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage< List< int>>" %> < asp:Content ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server"> Pager for List < /asp:Content> < asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server"> < div> < ul> < %foreach (int i in Model){//顯示這個(gè)列表%> < li>< %=i %>< /li> < %} %> < /ul> < /div> < !--將在這里顯示分頁的部分--> < /asp:Content>
下面是運(yùn)行后的結(jié)果:
ASP.NET MVC分頁控件:***頁URL類似/Home/Index?p=1
ASP.NET MVC分頁控件:第二頁URL類似/Home/Index?p=2
其它頁面以此類推
ASP.NET MVC分頁控件:最簡單的解決方案
我想最簡單無非就是直接寫鏈接,當(dāng)然也要考慮更換Routing規(guī)則的問題,所以我們可以最簡單如下來寫:
< % int p = 1; int.TryParse(Request.QueryString["p"], out p); %> < div> < %=Html.ActionLink("上一頁", "Index", new { p= p-1})%> < strong>當(dāng)前頁:< %=p %>< /strong> < %=Html.ActionLink("下一頁", "Index", new { p= p+1})%> < /div>
這樣就可以得到如果下的分頁樣式
當(dāng)然,也可以根據(jù)這個(gè)來寫1,2,3,4,5頁的鏈接,而不寫“上一頁”或“下一頁”
但是這種方法有個(gè)問題,就是使用Html.ActionLink的時(shí)候要用字符串來指定Action和Controller。下面我們來改換另一種方法來實(shí)現(xiàn)
使用RouteLink來實(shí)現(xiàn)
我們使用Html.RouteLink就可以實(shí)現(xiàn)不與Action或Controller的名稱相耦合,例如:
< %for (int i = 1; i < 10; i++) { ViewContext.RouteData.Values["p"] = i;//設(shè)置頁碼 Writer.Write( Html.RouteLink(i.ToString(), ViewContext.RouteData.Values) );//顯示設(shè)置頁面后的鏈接 Writer.Write(" ");//連接后顯示個(gè)空格,好看點(diǎn) }%>
這個(gè)列表,我們就可以顯示為
完善這個(gè)Pager并封裝成一個(gè)Helper
上面列出了Pager,但是有幾個(gè)問題
1.沒有上下頁
2.沒有指定當(dāng)前頁的特殊顯示
3.每次調(diào)用時(shí)都要寫一次
4.如果QueryString有其它參數(shù)時(shí)無法處理
那我們下面來完善這個(gè)Pager
并將之封裝成一個(gè)Helper
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Routing; using System.Text; using System.Web.Mvc.Html; namespace MvcApplication2.Helpers { public static class PagerExtensions { /**//// < summary> /// 分頁P(yáng)ager顯示 /// < /summary> /// < param name="html">< /param> /// < param name="currentPageStr">標(biāo)識(shí)當(dāng)前頁碼的QueryStringKey< /param> /// < param name="pageSize">每頁顯示< /param> /// < param name="totalCount">總數(shù)據(jù)量< /param> /// < returns>< /returns> public static string Pager(this HtmlHelper html, string currentPageStr, int pageSize, int totalCount) { var queryString = html.ViewContext.HttpContext.Request.QueryString; int currentPage = 1; //當(dāng)前頁 var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1); //總頁數(shù) var dict = new System.Web.Routing.RouteValueDictionary(html.ViewContext.RouteData.Values); var output = new System.Text.StringBuilder(); if (!string.IsNullOrEmpty(queryString[currentPageStr])) { //與相應(yīng)的QueryString綁定 foreach (string key in queryString.Keys) if (queryString[key] != null && !string.IsNullOrEmpty(key)) dict[key] = queryString[key]; int.TryParse(queryString[currentPageStr], out currentPage); } else { //獲取 ~/Page/{page number} 的頁號(hào)參數(shù) int.TryParse(dict[currentPageStr].ToString(), out currentPage); } if (currentPage < = 0) currentPage = 1; if (totalPages > 1) { if (currentPage != 1) { //處理首頁連接 dict[currentPageStr] = 1; output.AppendFormat("{0} ", html.RouteLink("首頁", dict)); } if (currentPage > 1) { //處理上一頁的連接 dict[currentPageStr] = currentPage - 1; output.Append(html.RouteLink("上一頁", dict)); } else { output.Append("上一頁"); } output.Append(" "); int currint = 5; for (int i = 0; i < = 10; i++) { //一共最多顯示10個(gè)頁碼,前面5個(gè),后面5個(gè) if ((currentPage + i - currint) >= 1 && (currentPage + i - currint) < = totalPages) if (currint == i) { //當(dāng)前頁處理 output.Append(string.Format("[{0}]", currentPage)); } else { //一般頁處理 dict[currentPageStr] = currentPage + i - currint; output.Append(html.RouteLink((currentPage + i - currint).ToString(), dict)); } output.Append(" "); } if (currentPage < totalPages) { //處理下一頁的鏈接 dict[currentPageStr] = currentPage + 1; output.Append(html.RouteLink("下一頁", dict)); } else { output.Append("下一頁"); } output.Append(" "); if (currentPage != totalPages) { dict[currentPageStr] = totalPages; output.Append(html.RouteLink("末頁", dict)); } output.Append(" "); } output.AppendFormat("{0} / {1}", currentPage, totalPages);//這個(gè)統(tǒng)計(jì)加不加都行 return output.ToString(); } }
添加Controller代碼:
public ActionResult Index(int? page) { if (page == null) page = 1; return View(); }
aspx頁面調(diào)用:
< %=Html.Pager("page", 10, 10020)%>
效果
這回我們算是解決了這個(gè)問題,ASP.NET MVC分頁控件成功構(gòu)建。
“ASP.NET MVC分頁控件的實(shí)現(xiàn)方法”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
網(wǎng)站題目:ASP.NETMVC分頁控件的實(shí)現(xiàn)方法
文章起源:http://chinadenli.net/article8/ihesip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、網(wǎng)站導(dǎo)航、品牌網(wǎng)站建設(shè)、網(wǎng)站內(nèi)鏈、軟件開發(fā)、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)