标签:
1 /// <summary> 2 /// 生成页码的html 3 /// </summary> 4 /// <param name="urlFormat">超链接的格式。list.ashx?pagenum={pagenum}。地址中用{pagenum}做为当前页码的占位符</param> 5 /// <param name="totalSize">总数据条数</param> 6 /// <param name="pageSize">每页多少条数据 </param> 7 /// <param name="currentPage">当前页的页码</param> 8 /// <returns></returns> 9 public static RawString Pager(string urlFormat, long totalSize, 10 long pageSize, long currentPage) 11 { 12 StringBuilder sb = new StringBuilder(); 13 //currentPage当前页的页面。在当前页之前显示最多5个、之后显示最多5个。 14 // 15,16,17,18,19,(20),21,22,23,24,25 15 // 1,2,(3),4,5,6,7,8 16 //一共50页, 43,44,45,46,,47(48),49,50 17 //for(int i=) 18 19 //总页数 20 long totalPageCount = (long)Math.Ceiling((totalSize*1.0f)/(pageSize*1.0f)); 21 //58*1.0f/10*1.0f=5.8,6 22 //60*1.0f/10*.1.f=6 23 //61*1.0f/10*1.0f=6.1=7 24 25 //在当前页面前后各最多显示5个页码 26 //计算页码条中第一条的页码 27 long firstPageNum = Math.Max(currentPage - 5, 1); 28 //计算页码条中最后一条的页码 29 long lastPageNum = Math.Min(currentPage + 5, totalPageCount); 30 31 sb.AppendLine("<li><a href=‘" + 32 urlFormat.Replace("{pagenum}","1") + "‘>首页</a></li>"); 33 for (long i = firstPageNum; i <= lastPageNum; i++) 34 { 35 string url = urlFormat.Replace("{pagenum}",i.ToString()); 36 if (i == currentPage) 37 { 38 sb.Append("<li class=‘active‘><a>" + i + "</a></li>"); 39 } 40 else 41 { 42 sb.Append("<li><a href=‘" + url + "‘>" + i + "</a></li>"); 43 } 44 } 45 sb.AppendLine("<li><a href=‘" + 46 urlFormat.Replace("{pagenum}", totalPageCount.ToString()) + "‘>末页</a></li>"); 47 return new RawString(sb.ToString()); 48 }
标签:
原文地址:http://www.cnblogs.com/Tan-sir/p/4841476.html