分页
不论做什么项目呢,只要是显示数据的都离不开分页的,想必大家都深有同感……这是我们写的一个分页,想与大家分享,仅供参考哈……
View Code
1 //设计页面 2 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="StudyPager.index" %> 3 4 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 5 6 <html xmlns="http://www.w3.org/1999/xhtml"> 7 <head runat="server"> 8 <title></title> 9 <link href="public.css" rel="stylesheet" type="text/css" /> 10 <script type="text/javascript"> 11 function page(pageno) { 12 // this.location.href = "index.aspx?p=" + pageno; 13 var form = document.getElementById("form1"); 14 form.p.value = pageno; 15 form.submit(); 16 } 17 </script> 18 </head> 19 <body> 20 <div id="WebPage" runat="server"></div> 21 </body> 22 </html> 23 24 /*源代码*/ 25 using System; 26 using System.Collections.Generic; 27 using System.Linq; 28 using System.Web; 29 using System.Web.UI; 30 using System.Web.UI.WebControls; 31 using System.Text; 32 namespace StudyPager 33 { 34 public partial class index : System.Web.UI.Page 35 { 36 protected void Page_Load(object sender, EventArgs e) 37 { 38 if (Request.Form["subflag"] != null && Request.Form["subflag"] != "" && Request.Form["subflag"] == "1") 39 { 40 int p = Convert.ToInt32(Request.Form["p"]); 41 StringBuilder sb = new StringBuilder(); 42 sb.Append(@"<form id=""form1"" action=""index.aspx"" method=""post""> 43 <input type=""hidden"" value=""1"" name=""p""/> 44 <input type=""hidden"" value=""1"" name=""subflag""/> 45 </form>"); 46 sb.Append(this.GetPagerHtml(121, 5, p, "pages1")); 47 this.WebPage.InnerHtml = sb.ToString(); 48 } 49 else 50 { 51 StringBuilder sb = new StringBuilder(); 52 sb.Append(@"<form id=""form1"" action=""index.aspx"" method=""post""> 53 <input type=""hidden"" value=""1"" name=""p""/> 54 <input type=""hidden"" value=""1"" name=""subflag""/> 55 </form>"); 56 sb.Append(this.GetPagerHtml(121, 5, 1, "pages1")); 57 this.WebPage.InnerHtml = sb.ToString(); 58 } 59 } 60 /// <summary> 61 /// 分页效果 62 /// </summary> 63 /// <param name="total">总记录数</param> 64 /// <param name="size">每页显示的条数</param> 65 /// <param name="page">当前的页码</param> 66 /// <param name="css">分页效果</param> 67 /// <returns></returns> 68 protected StringBuilder GetPagerHtml(int total,int size,int page,string css) 69 { 70 StringBuilder sb = new StringBuilder(); 71 sb.Append(@"<div id=""pager"" class="""+css+ @""">"); 72 //总页数 73 int allpage = 0; 74 if (size != 0) 75 { 76 //整除 77 allpage = total / size; 78 //非整除 79 allpage = ((total % size) != 0 ? allpage+1 : allpage); 80 //和0比较 81 allpage = total == 0 ? 1 : allpage; 82 } 83 //判断页码和总页数 84 if (page > allpage) 85 { 86 page = allpage; 87 } 88 if (page < 1) 89 { 90 page = 1; 91 } 92 //上一页和下一页 93 int pre = page - 1; 94 int next = page + 1; 95 96 //记录分页位置(分页偏移量) 97 int startcount = 0; 98 if (startcount <= 0) 99 { 100 startcount = 1; 101 } 102 #region 判断分页 103 /*---------------设置上一页--------------------*/ 104 if (page > 1) 105 { 106 sb.Append(@"<a disabled=""disabled"" href=""javascript:page(" + pre + @")"" style=""margin-right:5px;"">上一页</a>"); 107 } 108 else 109 { 110 sb.Append(@"<span class=""cpb"" style=""margin-right:5px;"">上一页</span>"); 111 } 112 113 #region 分页开始 114 if (allpage < 10) 115 { 116 for (int i = 1; i <= allpage; i++) 117 { 118 if (page == i) 119 { 120 sb.Append(@"<span class=""cpb"" style=""margin-right:5px;"">" + i + @"</span>"); 121 } 122 else 123 { 124 sb.Append(@"<a disabled=""disabled"" href=""javascript:page(" + i + @")"" style=""margin-right:5px;"">" + i + @"</a>"); 125 } 126 } 127 } 128 else 129 { 130 startcount = (page+5) > allpage? allpage-10 : page-5; 131 #region 当前页小于10 132 if (page < 10) 133 { 134 for (int i = 1; i <= 10; i++) 135 { 136 if (page == i) 137 { 138 sb.Append(@"<span class=""cpb"" style=""margin-right:5px;"">" + i + @"</span>"); 139 } 140 else 141 { 142 sb.Append(@"<a disabled=""disabled"" href=""javascript:page(" + i + @")"" style=""margin-right:5px;"">" + i + @"</a>"); 143 } 144 } 145 } 146 else { 147 //如果不是最后10页 148 if (page < allpage - 10) 149 { 150 for (int i = startcount; i < startcount + 10; i++) 151 { 152 if (page == i) 153 { 154 sb.Append(@"<span class='cpb' style='margin-right:5px;'>" + i + @"</span>"); 155 } 156 else 157 { 158 sb.Append(@"<a href=""javascript:page(" + i + @")"" style='margin-right:5px;'>" + i + @"</a>"); 159 } 160 } 161 } 162 else 163 { 164 for (int i = allpage - 10; i <= allpage; i++) 165 { 166 if (page == i) 167 { 168 sb.Append(@"<span class=""cpb"" style=""margin-right:5px;"">" + i + @"</span>"); 169 } 170 else 171 { 172 sb.Append(@"<a disabled=""disabled"" href=""javascript:page(" + i + @")"" style=""margin-right:5px;"">" + i + @"</a>"); 173 } 174 } 175 } 176 177 } 178 179 #endregion 180 } 181 182 #endregion 183 /*---------------设置下一页--------------------*/ 184 if (page < allpage) 185 { 186 sb.Append(@"<a disabled=""disabled"" href=""javascript:page(" + next + @")"" style=""margin-right:5px;"">下一页</a>"); 187 } 188 else 189 { 190 sb.Append(@"<span class=""cpb"" style=""margin-right:5px;"">下一页</span>"); 191 } 192 #endregion 193 194 sb.Append(@"</div>"); 195 return sb; 196 } 197 } 198 } 199 200 201 //页面CSS 202 /*------------------------------------分页开始---------------------------------------------*/ 203 /**分页样式1*/ 204 .pages2 { font: 12px Arial, Helvetica, sans-serif;padding:10px 20px 10px 0; margin: 0px;} 205 .pages2 a {padding: 1px 6px; border: solid 1px #ddd; background: #fff; text-decoration: none;margin-right:2px} 206 .pages2 a:visited {padding: 1px 6px; border: solid 1px #ddd; background: #fff; text-decoration: none;} 207 .pages2 .cpb {padding: 1px 6px;font-weight: bold; font-size: 12px;border:none} 208 .pages2 a:hover {color: #fff; background: #ffa501;border-color:#ffa501;text-decoration: none;} 209 /**分页样式2*/ 210 211 .pages1 .cpb {font-size:12px;background:#1F3A87 none repeat scroll 0 0;border:1px solid #CCCCCC;color:#FFFFFF;font-weight:bold;margin:5px 4px 0 0;padding:4px 5px 0;} 212 .pages1 a {background:#FFFFFF none repeat scroll 0 0;border:1px solid #CCCCCC;color:#1F3A87;margin:5px 4px 0 0;padding:4px 5px 0;text-decoration:none;font-size:12px} 213 .pages1 a:hover{background:#1F3A87 none repeat scroll 0 0;border:1px solid #1F3A87;color:#FFFFFF;font-size:14px} 214 215 /**分页样式3*/ 216 .pages { color: #999; } 217 .pages a, .pages .cpb { text-decoration:none;float: left; padding: 0px 5px; border: 1px solid #ddd;background: #ffff;margin:0 2px; font-size:12px; color:#000;} 218 .pages a:hover { background-color: #E61636; color:#fff;border:1px solid #E61636; text-decoration:none;} 219 .pages .cpb { font-weight: bold; color: #fff; background: #E61636; border:1px solid #E61636;} 220 /*--------------------END--------------------*/