標籤:class blog http ext string os
HtmlHelper的擴充:注意點:擴充方法必須是靜態方法,所在的類必須是靜態類,所在的命名空間改成System.Web.MVC則能省略頁面中必須添加命名空間的約束。//主要就是輸出分頁的超級連結的標籤//自訂分頁Helper擴充public static HtmlString ShowPageNavigate(this HtmlHelper htmlHelper, int currentPage, int pageSize, int totalCount) { var redirectTo = htmlHelper.ViewContext.RequestContext.HttpContext.Request.Url.AbsolutePath; pageSize = pageSize == 0 ? 3 : pageSize; var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1); //總頁數 var output = new StringBuilder(); if (totalPages > 1) { //if (currentPage != 1) {//處理首頁串連 output.AppendFormat("<a class=‘pageLink‘ href=‘{0}?pageIndex=1&pageSize={1}‘>首頁</a> ", redirectTo, pageSize); } if (currentPage > 1) {//處理上一頁的串連 output.AppendFormat("<a class=‘pageLink‘ href=‘{0}?pageIndex={1}&pageSize={2}‘>上一頁</a> ", redirectTo, currentPage - 1, pageSize); } else { // output.Append("<span class=‘pageLink‘>上一頁</span>"); } output.Append(" "); int currint = 5; for (int i = 0; i <= 10; i++) {//一共最多顯示10個頁碼,前面5個,後面5個 if ((currentPage + i - currint) >= 1 && (currentPage + i - currint) <= totalPages) { if (currint == i) {//當前頁處理 //output.Append(string.Format("[{0}]", currentPage)); output.AppendFormat("<a class=‘cpb‘ href=‘{0}?pageIndex={1}&pageSize={2}‘>{3}</a> ", redirectTo, currentPage , pageSize, currentPage ); } else {//一般頁處理 output.AppendFormat("<a class=‘pageLink‘ href=‘{0}?pageIndex={1}&pageSize={2}‘>{3}</a> ", redirectTo, currentPage + i - currint, pageSize, currentPage + i - currint); } } output.Append(" "); } if (currentPage < totalPages) {//處理下一頁的連結 output.AppendFormat("<a class=‘pageLink‘ href=‘{0}?pageIndex={1}&pageSize={2}‘>下一頁</a> ", redirectTo, currentPage + 1, pageSize); } else { //output.Append("<span class=‘pageLink‘>下一頁</span>"); } output.Append(" "); if (currentPage != totalPages) { output.AppendFormat("<a class=‘pageLink‘ href=‘{0}?pageIndex={1}&pageSize={2}‘>末頁</a> ", redirectTo, totalPages, pageSize); } output.Append(" "); } output.AppendFormat("第{0}頁 / 共{1}頁", currentPage, totalPages);//這個統計加不加都行 return new HtmlString(output.ToString()); }