.NET 自定义用户控件分页
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <%if(total>0&&totalPage>0){%> 2 <div class="dataTables_info"> 3 共 <strong><%=total %></strong> 条 4 <label> 5 每页显示 6 <select class="select" style="width: 50px" onchange="javascript:location.href='<%=pageUrl %>pagesize='+this.options[this.options.selectedIndex].value" > 7 <option value="10" <%=(pageSize==10)?"selected='selected'":"" %>>10</option> 8 <option value="20" <%=(pageSize==20)?"selected='selected'":"" %>>20</option> 9 <option value="50" <%=(pageSize==50)?"selected='selected'":"" %>>50</option> 10 <option value="100" <%=(pageSize==100)?"selected='selected'":"" %>>100</option> 11 <option value="200" <%=(pageSize==200)?"selected='selected'":"" %>>200</option> 12 <option value="300" <%=(pageSize==300)?"selected='selected'":"" %>>300</option> 13 </select> 14 条</label> 15 </div> 16 <div class="dataTables_paginate paging_simple_numbers"> 17 <%if(pageIndex>1){ %> 18 <a class="paginate_button" href="<%=pageUrl %>page=1&pagesize=<%=pageSize %>&total=<%=total %>">首页</a> 19 <a class="paginate_button" href="<%=pageUrl %>page=<%=pageIndex-1%>&pagesize=<%=pageSize %>&total=<%=total %>">上一页</a> 20 <%} %> 21 22 <%for (int i = 1; i <= totalPage; i++){if (Math.Abs(pageIndex - i) <= 2 || (pageIndex <= 2 && i <= 5) || ((totalPage - pageIndex) < 2) && (totalPage - i) < 5){ %> 23 <%if (i == pageIndex){ %> 24 <span><a class="paginate_button current"><%=i %></a></span> 25 <%}else{ %> 26 <a class="paginate_button" href="<%=pageUrl %>pagesize=<%=pageSize %>&page=<%=i %>&total=<%=total %>"><%=i %></a> 27 <%} %> 28 <%}}%> 29 30 <%if(pageIndex<totalPage){ %> 31 <a class="paginate_button" href="<%=pageUrl %>page=<%=pageIndex+1 %>&pagesize=<%=pageSize %>&total=<%=total %>">下一页</a> 32 <a class="paginate_button" href="<%=pageUrl %>page=<%=totalPage %>&pagesize=<%=pageSize %>&total=<%=total %>">末页</a> 33 <%} %> 34 </div> 35 <%} %>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 4 namespace FishAdmin.usercontrols 5 { 6 public partial class UC_Pagination : System.Web.UI.UserControl 7 { 8 /// <summary> 9 /// 当前页码 10 /// </summary> 11 protected int pageIndex; 12 /// <summary> 13 /// 每页显示数量 14 /// </summary> 15 protected int pageSize; 16 /// <summary> 17 /// 共计多少页 18 /// </summary> 19 protected int totalPage; 20 /// <summary> 21 /// 总数据数量 22 /// </summary> 23 protected int total; 24 /// <summary> 25 /// 页面路径 26 /// </summary> 27 protected string pageUrl; 28 29 protected void Page_Load(object sender, EventArgs e) 30 { 31 32 } 33 34 public void ShowPage(int _pageIndex, int _pageSize, int _total, Dictionary<string, string> _dicParam = null, string _pageUrl = "") 35 { 36 if (_total <= 0) 37 return; 38 if (_pageIndex <= 1) 39 _pageIndex = 1; 40 if (_pageSize <= 0) 41 _pageSize = 10; 42 43 if (string.IsNullOrEmpty(_pageUrl)) 44 _pageUrl = Request.Url.AbsolutePath; 45 46 if (_pageUrl.Contains("?")) 47 _pageUrl += "&"; 48 else 49 _pageUrl += "?"; 50 if (null != _dicParam) 51 { 52 string strParam = string.Empty; 53 foreach (KeyValuePair<string, string> kv in _dicParam) 54 { 55 if (string.IsNullOrEmpty(kv.Key) || string.IsNullOrEmpty(kv.Value)) 56 continue; 57 strParam += kv.Key + "=" + kv.Value + "&"; 58 } 59 strParam = strParam.Trim('&'); 60 if (!string.IsNullOrEmpty(strParam)) 61 _pageUrl += strParam + "&"; 62 } 63 64 this.pageIndex = _pageIndex; 65 this.pageSize = _pageSize; 66 this.total = _total; 67 this.totalPage = _total % _pageSize == 0 ? _total / _pageSize : _total / _pageSize + 1; 68 this.pageUrl = _pageUrl; 69 } 70 } 71 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 protected void Page_Load(object sender, EventArgs e) 2 { 3 base.CheckPagePermission("18_Access"); 4 if (!IsPostBack) 5 { 6 ShowData(); 7 } 8 } 9 10 private void ShowData() 11 { 12 if (roomtype < 100) 13 return; 14 15 DataTable dt = null; 16 CDatabase db = new CDatabase("FPlatformDB"); 17 try 18 { 19 dt = FishAdo.p_oa_fish_config_list(roomtype, fishtype, page, pagesize, ref total, db); 20 } 21 catch (Exception ex) 22 { 23 CLog.WriteLog("fish_config_list.aspx->ShowData Message:{0},StackTrace:{1}", ex.Message, ex.StackTrace); 24 } 25 finally { db.close(); } 26 //此处表示为将dt 数据赋值给repeart 控件 27 BindHelper.BindData(dt, this.repList); 28 29 //分页所传递参数 30 Dictionary<string, string> dicParam = new Dictionary<string, string>(); 31 dicParam.Add("roomtype", roomtype.ToString()); 32 dicParam.Add("fishtype", fishtype.ToString()); 33 34 //page 页码 35 //pahesize 每页数量 36 // total 总数 37 //UC_Pagination 为前端分页用户控件名称,ShowPage 为用户 38 控件定义的方法名 39 this.UC_Pagination.ShowPage(page, pagesize, total, dicParam); 40 }