資源描述:
《一個asp net MVC 的分頁代碼》由會員上傳分享,免費在線閱讀,更多相關內(nèi)容在教育資源-天天文庫。
1、一個asp.netMVC的分頁代碼 首先聲明,這個分頁代碼并不是出自我手哈,借用了網(wǎng)上的一段代碼,然后加了個css,變的好看一些啦~~ 原作者忘記是誰了,實在找不到了……萬分抱歉啊~~ 效果如下: 復制代碼 1usingSystem; 2usingSystem.Collections.Generic; 3usingSystem.Linq; 4usingSystem.Web; 5usingSystem.Web.Mvc; 6usingSystem.Web.Routing; 7usingSystem.Text; 8usingSys
2、tem.Web.Mvc.Html; 9namespaceapplication.Helpers 10{ 11publicstaticclassPagerExtensions 12{ 13/*helper分頁 14*使用方式:<%@ImportNamespace="ExpoShiep.Helpers"%> 15*<%=Html.Pager("page",10,100)%>*/ 16/// 17///分頁Pager顯示 18/// 19///
3、 20///標識當前頁碼的QueryStringKey 21///每頁顯示b31.org 22///總數(shù)據(jù)量 23/// 24publicstaticstringPager(thisHtmlHelperhtml,stringcurrentPageStr,intpageSize,inttotalCount)
4、25{ 26varqueryString=html.ViewContext.HttpContext.Request.QueryString; 27intcurrentPage=1;//當前頁 28vartotalPages=Math.Max((totalCount+pageSize-1)/pageSize,1);//總頁數(shù) 29vardict=newSystem.Web.Routing.RouteValueDictionary(html.ViewContext.RouteData.Values); 30varoutput=newSyste
5、m.Text.StringBuilder(); 31if(!string.IsNullOrEmpty(queryString[currentPageStr])) 32{//與相應的QueryString綁定 33foreach(stringkeyinqueryString.Keys) 34if(queryString[key]!=null&&!string.IsNullOrEmpty(key)) 35dict[key]=queryString[key]; 36int.TryParse(queryString[currentPageStr
6、],outcurrentPage); 37} 38else 39{//獲取~/Page/{pagenumber}的頁號參數(shù) 40int.TryParse(dict[currentPageStr].ToString(),outcurrentPage); 41} 42if(currentPage<=0) 43currentPage=1; 44if(totalPages>1) 45{ 46if(currentPage!=1) 47{//處理首頁連接 48dict[currentPageStr]=1; 49output.Appen
7、dFormat("{0}",html.RouteLink("首頁",dict)); 50} 51if(currentPage>1) 52{//處理上一頁的連接 53dict[currentPageStr]=currentPage-1; 54output.Append(html.RouteLink("上一頁",dict)); 55} 56else 57{ 58//output.Append("上一頁"); 59dict[currentPageStr]=currentPage; 60output.Append(html.Route
8、Link("上一頁",dict)); 61} 62output.Append(""); 63intcurrint=5