.net 分页控件
(1)页面调用
<%=ViewData["GotoPage"]%>
(2)Controller 代码
1 int pageSize = 8; 2 int recordCount = 0; 3 int pageIndex = FormHelper.GetInt("PageNo"); 4 pageIndex = pageIndex == 0 ? 1 : pageIndex; 5 ViewData["pageIndex"] = pageIndex; 6 Dictionary<String, Object> where = new Dictionary<string, object>(); 7 where.Add("IsCover", true); 8 Dictionary<String, Object> like = new Dictionary<string, object>(); 9 IList<Simpletemplate> sList = SimpletemplateAPI.Instance.GetPagerList(where, null, null, pageSize, pageIndex, out recordCount); 10 ViewData["sList"] = sList; 11 12 // 分页 13 PagerHelper page = new PagerHelper(pageIndex, pageSize, recordCount); 14 ViewData["Pager"] = page; 15 ViewData["RecordCount"] = recordCount; 16 string gotopage = HtmlWapHelper.PageGotoList(recordCount, pageSize, pageIndex, "/Template/Index/?1=1"); 17 ViewData["GotoPage"] = gotopage;
(3)分页帮助类
1 public class PagerHelper 2 { 3 4 public PagerHelper(int curpage, int psize, int tolrows, int pageRow) 5 { 6 _pageSize = psize; 7 _currentPage = curpage; 8 _totalRows = tolrows; 9 _pageRow = pageRow; 10 } 11 public PagerHelper(int curpage, int psize, int tolrows) 12 { 13 _pageSize = psize; 14 _currentPage = curpage; 15 _totalRows = tolrows; 16 } 17 18 private int _pageSize = 10; //每页默认10条数据 19 private int _pageRow = 10; //每页显示分页数 20 private int _currentPage = 1; //当前页 21 private int _totalRows = 0; //总数据数 22 private string _url = string.Empty; 23 private Dictionary<string, string> _dic = new Dictionary<string, string>();//参数 24 private string _squery = string.Empty; 25 private string _urlstr = string.Empty; 26 /// <summary> 27 /// 每页数据条数 28 /// </summary> 29 public int PageSize 30 { 31 get 32 { 33 return _pageSize; 34 } 35 set 36 { 37 _pageSize = value; 38 } 39 } 40 /// <summary> 41 /// URL 42 /// </summary> 43 public string Url 44 { 45 get 46 { 47 return _url; 48 } 49 set 50 { 51 _url = value; 52 } 53 } 54 public int TotalRows 55 { 56 get 57 { 58 return _totalRows; 59 } 60 } 61 /// <summary> 62 /// 总页数 63 /// </summary> 64 public int TotalPages 65 { 66 get 67 { 68 if ((_totalRows % _pageSize) == 0) 69 { 70 return (_totalRows / _pageSize); 71 } 72 return (_totalRows / _pageSize + 1); 73 } 74 } 75 76 public int CurrentPage 77 { 78 get 79 { 80 return _currentPage; 81 } 82 } 83 /// <summary> 84 /// 上一页 85 /// </summary> 86 public int PrevPage 87 { 88 get 89 { 90 //int temp = _currentPage % _pageRow; 91 //return (_currentPage <= _pageRow? 1:(_currentPage-temp)+1); 92 if (_currentPage == 1) 93 { 94 return 1; 95 } 96 return _currentPage - 1; 97 } 98 } 99 /// <summary> 100 /// 下一页 101 /// </summary> 102 public int NextPage 103 { 104 get 105 { 106 //int temp = _currentPage + _pageRow - _currentPage % _pageRow + 1; 107 //return (temp >= totalPages ? totalPages : temp); 108 if (_currentPage > TotalPages) 109 { 110 return TotalPages; 111 } 112 return _currentPage + 1; 113 114 } 115 116 } 117 /// <summary> 118 /// 变量字典 119 /// </summary> 120 public Dictionary<string, string> Dic 121 { 122 set 123 { 124 _dic = value; 125 } 126 get 127 { 128 return _dic; 129 } 130 } 131 132 public string Urlstr 133 { 134 get 135 { 136 return string.Format("{0}?{1}", Url, Squery); 137 } 138 } 139 140 public string Squery 141 { 142 get 143 { 144 if (Dic.Count == 0) 145 { 146 return ""; 147 } 148 _squery = string.Empty; 149 foreach (string skey in Dic.Keys) 150 { 151 _squery = string.Format("{0}{1}={2}&", _squery, skey, Dic[skey]); 152 } 153 if (_squery.Length != 0) 154 { 155 _squery = _squery.Substring(0, _squery.Length - 1); 156 } 157 return _squery; 158 } 159 } 160 /// <summary> 161 /// 范围列表 162 /// </summary> 163 public List<int> ListNum 164 { 165 get 166 { 167 List<int> _listnum = new List<int>(); 168 int divPageRow = _pageRow % 2 == 0 ? _pageRow / 2 : (int)(_pageRow / 2) + 1; 169 170 int startIndex = _currentPage <= divPageRow ? 1 : _currentPage - divPageRow + 1; 171 172 int endIndex = _currentPage >= TotalPages ? TotalPages : _currentPage + divPageRow - (_pageRow % 2 == 0 ? 0 : 1); 173 if (_currentPage <= divPageRow) 174 { 175 endIndex = _pageRow >= TotalPages ? _pageRow : TotalPages; 176 } 177 if (_currentPage > _pageRow && TotalPages - startIndex < _pageRow) 178 { 179 startIndex = TotalPages - _pageRow + 1; 180 } 181 if (TotalPages > _pageRow && _currentPage <= divPageRow) 182 { 183 endIndex = _pageRow; 184 } 185 else if (endIndex >= TotalPages) 186 { 187 endIndex = TotalPages; 188 startIndex = (endIndex - _pageRow + 1) < 1 ? 1 : (endIndex - _pageRow + 1); 189 } 190 for (int num = startIndex; num <= endIndex; num++) 191 { 192 _listnum.Add(num); 193 } 194 return _listnum; 195 } 196 } 197 198 }
(4)获取分页html代码
public class HtmlWapHelper { #region public static string PageGotoList(int totalNum, int pageSize, int currentPage, string reUrl) /// <summary> /// 获得分页Html代码 /// </summary> /// <param name="totalNum">总条数</param> /// <param name="pageSize">每页显示的总条数</param> /// <param name="currentPage">当前页</param> /// <param name="reUrl">生成的url地址连接</param> /// <returns>分页Html代码</returns> public static string PageGotoList(int totalNum, int pageSize, int currentPage, string reUrl) { //计算总页数 int totalPage = totalNum / pageSize; //如果超额页数+1 if (totalNum % pageSize != 0) { totalPage = totalPage + 1; } //如果当前页小于1将赋值为1 if (currentPage < 1) currentPage = 1; //如果当前页大于总页数将赋值为总页数 if (currentPage > totalPage) currentPage = totalPage; int NextPage = 0; int PrevPage = 0; //int PageStart = 0; //int PageEnd = 0; StringBuilder PageStr = new StringBuilder(); NextPage = currentPage + 1; PrevPage = currentPage - 1; //PageStart = (currentPage + 5) > totalPage ? totalPage - 9 : currentPage - 4;//中间页起始序号 //PageEnd = currentPage < 5 ? 10 : currentPage + 5;//中间页终止序号 //if (PageStart < 1) { PageStart = 1; } //为了避免输出的时候产生负数,设置如果小于1就从序号1开始 //if (totalPage < PageEnd) { PageEnd = totalPage; }//页码+5的可能性就会产生最终输出序号大于总页码,那么就要将其控制在页码数之内 PageStr.AppendFormat(currentPage < totalPage ? "<a href=\"#{0}&PageNo={1}\" class=\"b\">下一页</a>" : "下一页", reUrl, NextPage.ToString()); PageStr.AppendFormat(currentPage > 1 ? " <a href=\"#{0}&PageNo={1}\" class=\"b\">上一页</a> <a href=\"#{0}&PageNo=1\" class=\"b\">首页</a>" : " 上一页 首页", reUrl, PrevPage.ToString(), totalPage.ToString()); PageStr.AppendFormat(" {0}/{1}页", currentPage, totalPage); return PageStr.ToString(); } #endregion }