开发一个简单,美观,实用,高效的分页控件___MXQ
好久没写文章了,今天补一篇2012-03-16 14:18:33
下载链接
https://files.cnblogs.com/MSMXQ/MXQ_URL%E5%88%86%E9%A1%B5%E7%99%BE%E4%B8%87%E7%BA%A7.rar
分页效果:
废话不多讲,直接上代码
1.建立一个用户控件
1 <%@ Control Language="C#" AutoEventWireup="true" CodeFile="pagination1.ascx.cs" Inherits="userControl_pagination1" %> 2 <style type="text/css"> 3 .pagination{ 4 overflow:hidden; 5 margin:0; 6 padding:10px 10px 6px 10px; 7 _zoom:1; 8 } 9 .pagination *{ 10 display:inline; 11 float:left; 12 margin:0; 13 padding:0; 14 font-size:12px; 15 } 16 .currentPage b{ 17 float:none; 18 color:#f00; 19 } 20 .pagination li{ 21 list-style:none; 22 margin:0 5px; 23 } 24 .pagination li li{ 25 position:relative; 26 margin:-2px 0 0; 27 font-family: Arial, Helvetica, sans-serif 28 } 29 .firstPage a,.previousPage a,.nextPage a,.lastPage a{ 30 overflow:hidden; 31 height:0; 32 text-indent:-9999em; 33 border-top:8px solid #fff; 34 border-bottom:8px solid #fff; 35 } 36 .pagination li li a{ 37 margin:0 1px; 38 padding:0 4px; 39 border:3px double #fff; 40 +border-color:#eee; 41 background:#eee; 42 color:#06f; 43 text-decoration:none; 44 } 45 .pagination li li a:hover{ 46 background:#f60; 47 border-color:#fff; 48 +border-color:#f60; 49 color:#fff; 50 } 51 li.firstPage{ 52 margin-left:40px; 53 border-left:3px solid #06f; 54 } 55 .firstPage a,.previousPage a{ 56 border-right:12px solid #06f; 57 } 58 .firstPage a:hover,.previousPage a:hover{ 59 border-right-color: #f60; 60 } 61 .nextPage a,.lastPage a{ 62 border-left:12px solid #06f; 63 } 64 .nextPage a:hover,.lastPage a:hover{ 65 border-left-color:#f60; 66 } 67 li.lastPage{ 68 border-right:3px solid #06f; 69 } 70 li li.currentState a{ 71 position:relative; 72 margin:-1px 3px; 73 padding:1px 4px; 74 border:3px double #fff; 75 +border-color:#f60; 76 background:#f60; 77 color:#fff; 78 } 79 li.currentState,.currentState a,.currentState a:hover{ 80 border-color:#fff #ccc; 81 cursor:default; 82 } 83 </style> 84 <div class="pagination"> 85 <ul> 86 <li> 87 <%=paginationStr%><br /> 88 </li> 89 </ul> 90 </div>
2.用户控件后台代码
1 using System; 2 using System.Collections; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Web; 6 using System.Web.UI; 7 using System.Web.UI.WebControls; 8 using System.Text; 9 using System.Text.RegularExpressions; 10 12 13 public partial class userControl_pagination1 : System.Web.UI.UserControl 14 { 15 16 //分页字符 17 public StringBuilder paginationStr = new StringBuilder(); 18 19 //总分页数 20 private string page_Count; 21 public string Page_Count 22 { 23 set 24 { 25 Regex re = new Regex(@"^[1-9]\d*$"); 26 if (re.IsMatch(value)) 27 { 28 int data_count = Convert.ToInt32(value); 29 if (data_count % 20 != 0) 30 { 31 page_Count = string.Format("{0}", data_count / 20 + 1); 32 } 33 else 34 { 35 page_Count = string.Format("{0}", data_count / 20); 36 } 37 } 38 } 39 } 40 41 //显示分页文字是中文 还是英文 42 private string text_Type; 43 public string Text_Type 44 { 45 set 46 { 47 if (string.IsNullOrEmpty(string.Format("{0}",value))) 48 { 49 value = "CN"; 50 } 51 text_Type = value.ToUpper().Trim(); 52 } 53 } 54 55 //当前页的索引 56 public int pageIndex = 0; 57 58 //当前请求URL 59 public string url = string.Empty; 60 61 //原始URL 62 public string baseUrl = string.Empty; 63 64 //获取所有参数集合 65 public Hashtable urlParameters = new Hashtable(); 66 // 分页模式: 67 // style1: 10页以内 68 // 首页 上一页 1 2 3 4 6 7 8 9 10下一页 末页 当前2/共200页 69 70 // style2: 10页以上 71 // 首页 上一页 5 6 7 8 9 10 11 12 13 14 15 16 下一页 末页 当前2/共200页 72 73 //style3: 74 //home prev 1 2 3 4 5 6 7 8 9 10 next last 75 76 77 protected void Page_Load(object sender, EventArgs e) 78 { 79 80 } 81 82 public void Get_URL() 83 { 84 try 85 { 86 string urlParams = string.Empty; 87 url = Request.Url.AbsoluteUri; 88 baseUrl = url.Split('?').Length > 1 ? url.Substring(0, url.IndexOf('?')) : url; 89 urlParams = url.Split('?').Length > 1 ? url.Substring(url.IndexOf('?') + 1) : ""; 90 if (url.Split('?').Length > 0 && url.Split('?').Length < 3) 91 { 92 if (urlParams.Trim() != "") 93 { 94 if (urlParams.Split('&').Length > 0) 95 { 96 for (int i = 0; i < urlParams.Split('&').Length; i++) 97 { 98 urlParameters.Add(urlParams.Split('&')[i].Split('=')[0], urlParams.Split('&')[i].Split('=')[1]); 99 } 100 } 101 } 102 else 103 { 104 urlParameters.Add("page", 1); 105 } 106 } 107 SetPaginationNumber(Convert.ToInt32(urlParameters["page"])); 108 } 109 catch 110 { 111 Page.ClientScript.RegisterStartupScript(GetType(), Guid.NewGuid().ToString(), "alert('参数错误!');top.location.href='../404.aspx'", true); 112 Response.End(); 113 //throw; 114 } 115 } 116 117 public void SetPaginationNumber(int PageIndexNumber) 118 { 119 int page_Count_i = Convert.ToInt32(page_Count); 120 string parmsStr = string.Empty; 121 foreach (var item in urlParameters.Keys) 122 { 123 if (item.ToString() != "page") 124 { 125 parmsStr += "&" + item.ToString() + "=" + urlParameters[item].ToString(); 126 } 127 } 128 paginationStr.Append("<ul id=\"pagination-flickr\">"); 129 //首页 130 paginationStr.Append(PageIndexNumber != 1 ? string.Format("<li class=\"next\"><a href=\"{0}?page={1}{2}\">{3}</a></li>" , 131 baseUrl, 1, parmsStr,text_Type=="EN"?"home":"首页") : ""); 132 133 //上一页 134 if (PageIndexNumber < 2) 135 { 136 PageIndexNumber = 1; 137 } 138 paginationStr.Append(string.Format("<li class=\"next\"><a href=\"{0}?page={1}{2}\">{3}</a></li>", 139 baseUrl, PageIndexNumber == 1 ? 1 : PageIndexNumber - 1, parmsStr,text_Type=="EN"?"prev":"上一页")); 140 141 // 数字导航 142 if (page_Count_i <= 10) 143 { 144 Set_NumStr(1, page_Count_i, PageIndexNumber, parmsStr); 145 } 146 else 147 { 148 if (PageIndexNumber < 5) 149 { 150 Set_NumStr(1, 10, PageIndexNumber, parmsStr); 151 } 152 else if ((PageIndexNumber + 5) > page_Count_i) 153 { 154 Set_NumStr((page_Count_i - 9), page_Count_i+1, PageIndexNumber, parmsStr); 155 } 156 else 157 { 158 Set_NumStr(PageIndexNumber - 5, PageIndexNumber + 4, PageIndexNumber, parmsStr); 159 } 160 } 161 162 //下一页 163 if (PageIndexNumber >= page_Count_i) 164 { 165 PageIndexNumber = page_Count_i - 1; 166 } 167 paginationStr.Append(string.Format("<li class=\"next\"><a href=\"{0}?page={1}{2}\">{3}</a></li>", 168 baseUrl, PageIndexNumber + 1, parmsStr,text_Type == "EN" ? "next" : "下一页")); 169 170 //末页 171 paginationStr.Append(PageIndexNumber != page_Count_i - 1 172 ? string.Format("<li class=\"next\"><a href=\"{0}?page={1}{2}\">{3}</a></li>", 173 baseUrl, page_Count_i, parmsStr, text_Type == "EN" ? " last" : "末页") : ""); 174 175 //当前1/共20页 176 paginationStr.Append(string.Format("<li class=\"next\"><a href=\"#\">当前{0}/共{1}页</a></li>", PageIndexNumber, page_Count_i)); 177 paginationStr.Append("</ul>"); 178 } 179 180 //设置数字分页字符串 181 private void Set_NumStr(int BeginNumber, int EndNumber, int PageIndexNumber, string parmsStr) 182 { 183 for (int i = BeginNumber; i < EndNumber; i++) 184 { 185 paginationStr.Append(string.Format("<li {0}><a href=\"{1}?page={2}{3}\">{4}</a></li>", 186 i == PageIndexNumber ? "class=\"currentState\"" : "", baseUrl, i, parmsStr, i)); 187 } 188 } 189 }
3. 调用方法
建立一个aspx 页面,将上面的用户控件拖放到页面目标位置,相信这个大家都会
来看看后台是怎么使用的吧
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 using System.Data; 8 using System.Data.SqlClient; 9 10 11 public partial class userControl_Default2 : System.Web.UI.Page 12 { 13 public int page_index = 1; 14 protected void Page_Load(object sender, EventArgs e) 15 { 16 if (Request.QueryString["page"] != null) { 17 page_index = Convert.ToInt32(Request.QueryString["page"]); 18 } 19 table_bind2(); 20 } 21 22 protected void table_bind2() 23 { 24 string SqlStr = "SELECT COUNT(1) FROM sys_user"; 25 string data_count = string.Format("{0}", sqlHelper.ExecuteScalar(sqlHelper.sqlConnectonStr 26 , CommandType.Text, SqlStr, null)); 27 SqlStr = "SELECT TOP 5 * FROM sys_user WHERE usr_id >=(SELECT ISNULL(MAX(usr_id),0)FROM (SELECT TOP " 28 + (2 * (page_index - 1)) + " usr_id FROM sys_user ORDER BY usr_id ) A )"; 29 GridView1.DataSource = sqlHelper.ExecuteDataTable(sqlHelper.sqlConnectonStr, CommandType.Text, SqlStr, null); 30 GridView1.DataBind(); 31 pagination11.Page_Count = data_count; 32 pagination11.Text_Type = "CN"; 33 pagination11.Get_URL(); 34 } 35 }
|
请发表评论