麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 編程 > .NET > 正文

asp.net MVC分頁(yè)代碼分享

2024-07-10 13:31:23
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例為大家分享了MVC分頁(yè)代碼,供大家參考,具體內(nèi)容如下

using System.Collections.Generic;using System.Collections.Specialized;using System.Linq;using System.Web;using System.Text;using System.Web.Mvc;using System.Web.Routing;using System.Data.Objects.DataClasses;namespace System.Web.Mvc{ public static class PagerHelper {  /// <summary>  /// 分頁(yè)  /// </summary>  /// <param name="helper"></param>  /// <param name="id">分頁(yè)id</param>  /// <param name="currentPageIndex">當(dāng)前頁(yè)</param>  /// <param name="pageSize">分頁(yè)尺寸</param>  /// <param name="recordCount">記錄總數(shù)</param>  /// <param name="htmlAttributes">分頁(yè)頭標(biāo)簽屬性</param>  /// <param name="className">分頁(yè)樣式</param>  /// <param name="mode">分頁(yè)模式</param>  /// <returns></returns>  public static string Pager(this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount, object htmlAttributes, string className, PageMode mode)  {   TagBuilder builder = new TagBuilder("table");   builder.IdAttributeDotReplacement = "_";   builder.GenerateId(id);   builder.AddCssClass(className);   builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));   builder.InnerHtml = GetNormalPage(currentPageIndex, pageSize, recordCount, mode);   return builder.ToString();  }  /// <summary>  /// 分頁(yè)  /// </summary>  /// <param name="helper"></param>  /// <param name="id">分頁(yè)id</param>  /// <param name="currentPageIndex">當(dāng)前頁(yè)</param>  /// <param name="pageSize">分頁(yè)尺寸</param>  /// <param name="recordCount">記錄總數(shù)</param>  /// <param name="className">分頁(yè)樣式</param>  /// <returns></returns>  public static string Pager(this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount, string className)  {   return Pager(helper, id, currentPageIndex, pageSize, recordCount, null, className, PageMode.Normal);  }  /// <summary>  /// 分頁(yè)  /// </summary>  /// <param name="helper"></param>  /// <param name="id">分頁(yè)id</param>  /// <param name="currentPageIndex">當(dāng)前頁(yè)</param>  /// <param name="pageSize">分頁(yè)尺寸</param>  /// <param name="recordCount">記錄總數(shù)</param>  /// <returns></returns>  public static string Pager(this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount)  {   return Pager(helper, id, currentPageIndex, pageSize, recordCount, null);  }  /// <summary>  /// 分頁(yè)  /// </summary>  /// <param name="helper"></param>  /// <param name="id">分頁(yè)id</param>  /// <param name="currentPageIndex">當(dāng)前頁(yè)</param>  /// <param name="pageSize">分頁(yè)尺寸</param>  /// <param name="recordCount">記錄總數(shù)</param>  /// <param name="mode">分頁(yè)模式</param>  /// <returns></returns>  public static string Pager(this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount, PageMode mode)  {   return Pager(helper, id, currentPageIndex, pageSize, recordCount, null, mode);  }  /// <summary>  /// 分頁(yè)  /// </summary>  /// <param name="helper"></param>  /// <param name="id">分頁(yè)id</param>  /// <param name="currentPageIndex">當(dāng)前頁(yè)</param>  /// <param name="pageSize">分頁(yè)尺寸</param>  /// <param name="recordCount">記錄總數(shù)</param>  /// <param name="className">分頁(yè)樣式</param>  /// <param name="mode">分頁(yè)模式</param>  /// <returns></returns>  public static string Pager(this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount, string className, PageMode mode)  {   return Pager(helper, id, currentPageIndex, pageSize, recordCount, null, className, mode);  }  /// <summary>  /// 獲取普通分頁(yè)  /// </summary>  /// <param name="currentPageIndex"></param>  /// <param name="pageSize"></param>  /// <param name="recordCount"></param>  /// <returns></returns>  private static string GetNormalPage(int currentPageIndex, int pageSize, int recordCount, PageMode mode)  {   int pageCount = (recordCount % pageSize == 0 ? recordCount / pageSize : recordCount / pageSize + 1);   StringBuilder url = new StringBuilder();   url.Append(HttpContext.Current.Request.Url.AbsolutePath + "?page={0}");   NameValueCollection collection = HttpContext.Current.Request.QueryString;   string[] keys = collection.AllKeys;   for (int i = 0; i < keys.Length; i++)   {    if (keys[i].ToLower() != "page")     url.AppendFormat("&{0}={1}", keys[i], collection[keys[i]]);   }   StringBuilder sb = new StringBuilder();   sb.Append("<tr><td>");   sb.AppendFormat("總共{0}條記錄,共{1}頁(yè),當(dāng)前第{2}頁(yè) ", recordCount, pageCount, currentPageIndex);   if (currentPageIndex == 1)    sb.Append("<span>首頁(yè)</span> ");   else   {    string url1 = string.Format(url.ToString(), 1);    sb.AppendFormat("<span><a href={0}>首頁(yè)</a></span> ", url1);   }   if (currentPageIndex > 1)   {    string url1 = string.Format(url.ToString(), currentPageIndex - 1);    sb.AppendFormat("<span><a href={0}>上一頁(yè)</a></span> ", url1);   }   else    sb.Append("<span>上一頁(yè)</span> ");   if (mode == PageMode.Numeric)    sb.Append(GetNumericPage(currentPageIndex, pageSize, recordCount, pageCount, url.ToString()));   if (currentPageIndex < pageCount)   {    string url1 = string.Format(url.ToString(), currentPageIndex + 1);    sb.AppendFormat("<span><a href={0}>下一頁(yè)</a></span> ", url1);   }   else    sb.Append("<span>下一頁(yè)</span> ");    if (currentPageIndex == pageCount)    sb.Append("<span>末頁(yè)</span> ");   else   {    string url1 = string.Format(url.ToString(), pageCount);    sb.AppendFormat("<span><a href={0}>末頁(yè)</a></span> ", url1);   }   return sb.ToString();  }  /// <summary>  /// 獲取數(shù)字分頁(yè)  /// </summary>  /// <param name="currentPageIndex"></param>  /// <param name="pageSize"></param>  /// <param name="recordCount"></param>  /// <param name="pageCount"></param>  /// <param name="url"></param>  /// <returns></returns>  private static string GetNumericPage(int currentPageIndex, int pageSize, int recordCount, int pageCount, string url)  {   int k = currentPageIndex / 10;   int m = currentPageIndex % 10;   StringBuilder sb = new StringBuilder();   if (currentPageIndex / 10 == pageCount / 10)   {    if (m == 0)    {     k--;     m = 10;    }    else     m = pageCount % 10;   }   else    m = 10;   for (int i = k * 10 + 1; i <= k * 10 + m; i++)   {    if (i == currentPageIndex)     sb.AppendFormat("<span><font color=red><b>{0}</b></font></span> ", i);    else    {     string url1 = string.Format(url.ToString(), i);     sb.AppendFormat("<span><a href={0}>{1}</a></span> ", url1, i);    }   }   return sb.ToString();  } } /// <summary> /// 分頁(yè)模式 /// </summary> public enum PageMode {  /// <summary>  /// 普通分頁(yè)模式  /// </summary>  Normal,  /// <summary>  /// 普通分頁(yè)加數(shù)字分頁(yè)  /// </summary>  Numeric }}

html代碼

<div id="pageNav" class="pageinator">  @Html.ShowPageNavigate((int)ViewData["pageindex"], (int)ViewBag.pageSize, (int)ViewBag.totalCount); </div>

控制器

shopEntities shop = new shopEntities();  public ActionResult Index()  {   //IQueryable<tbl_order> order = (from a in shop.tbl_order select a).OrderBy(a=>a.id).Skip(10).Take(10);   //ViewData["order"] = order;   //return View();   int pageIndex = Request["pageIndex"] == null ? 1 : int.Parse(Request["pageIndex"]);   int pageSize = Request["pageSize"] == null ? 10 : int.Parse(Request["pageSize"]);   int totalCount = 0;   //給前臺(tái)傳遞 分頁(yè)的參數(shù)數(shù)據(jù)   ViewData["pageIndex"] = pageIndex;   //ViewData["pageSize"] = pageSize;   ViewBag.pageSize = pageSize;   //總條數(shù)   totalCount = shop.tbl_order.Count();   ViewBag.totalCount = totalCount;   //把當(dāng)前頁(yè)面數(shù)據(jù)發(fā)送到前臺(tái)。   //ViewData.Model = db.UserInfo.ToList();   //List<tbl_order> pp = shop.tbl_order   //     .OrderBy(u => u.id)   //     .Skip((pageIndex - 1) * pageSize)   //     .Take(pageSize).ToList();   IQueryable<tbl_order> pp = shop.tbl_order       .OrderBy(u => u.id)       .Skip((pageIndex - 1) * pageSize)       .Take(pageSize);   return View(pp);  } 

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到ASP.NET教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 欧美一级免费在线观看 | 最新午夜综合福利视频 | 久久精品一区二区三 | av电影观看 | 97视频 | 亚洲网站在线播放 | 欧美囗交 | 亚洲第一视频在线 | 视频一区二区三区视频 | 日本欧美一区二区三区在线观看 | 一级电影中文字幕 | 精品成人av一区二区三区 | 亚洲精品一区国产精品丝瓜 | 欧美日韩亚洲精品一区二区三区 | 日本欧美一区二区三区在线播 | 四虎久草| 欧美日韩免费一区 | 羞羞电影在线观看www | japanese xxxxhd | 久久久久久久一区二区 | 成人午夜一区二区 | 国产亚洲精品久久777777 | 激情小说激情图片激情电影 | 蜜桃一本色道久久综合亚洲精品冫 | 国产午夜精品一区二区三区四区 | 久久av免费 | 国产精品69久久 | 婷婷中文字幕一区二区三区 | 欧美成人性色区 | 午夜视频在线观看91 | 色就色 综合偷拍区91网 | 国产精品视频免费网站 | 91久久极品少妇韩国 | 轻点插视频 | 免费福利在线视频 | 精品久久一区二区 | 欧美日韩在线中文字幕 | 免费一级在线观看 | 久久婷婷一区二区三区 | 国产精品999在线观看 | 黄污网站在线观看 |