1. 程式人生 > 實用技巧 >.NET 自定義使用者控制元件分頁

.NET 自定義使用者控制元件分頁

 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 <%} %>
分頁控制元件前端
 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 }
分頁控制元件後端
 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         }
分頁方法頁面後端