先来个效果图:
样式代码:
1 .pagerbox { float: left; } 2 .pager { margin: 20px 0px; } 3 .pager span { line-height: 28px; float: right; } 4 .pagerbox a, .pagerbox b { background-color: #009AD9; border: 1px solid #009AD9; } 5 .pager .pagerbox a { color: #FFF; float: left; line-height: 28px; padding: 0 10px; margin-right: 2px; } 6 .pager .pagerbox a:hover { color: #000000; border: 1px solid #148ec0; background: #f0f0f0; } 7 .pager .pagerbox b { float: left; font-weight: normal; padding: 0 10px; background: #fff; line-height: 28px; margin-right: 2px; }
后台代码:
1 ///<summary> 2 ///打印分页信息。 3 ///</summary> 4 ///<param name="recordCount">记录总数。</param> 5 ///<param name="pageIndex">分页索引。</param> 6 ///<param name="pageSize">分页大小。</param> 7 ///<param name="pageLink">分页地址,{0}为当前页匹配。</param> 8 /// <param name="nextPageText">下一页显示文本。</param> 9 /// <param name="prevPageText">上一页显示文本。</param> 10 /// <param name="homePageText">首页显示文本。</param> 11 /// <param name="lastPageText">尾页显示文本。</param> 12 /// <param name="displayMsg">显示消息文本。</param> 13 /// <returns>返回分页的 Html 代码。</returns> 14 public static string Print(int recordCount, int pageIndex, int pageSize, string pageLink, string nextPageText, string prevPageText, string homePageText, string lastPageText, string displayMsg) 15 { 16 if (pageSize < 1) { pageSize = 1; } 17 if (pageIndex < 1) { pageIndex = 1; } 18 //开始 19 string html = string.Empty; 20 string htmlLeftPage = string.Empty; 21 string htmlRightPage = string.Empty; 22 if (pageLink == string.Empty) { pageLink = "?"; } 23 //开始计算 24 long pageCount = 0; 25 if (recordCount % pageSize == 0) 26 { 27 pageCount = recordCount / pageSize; 28 } 29 else 30 { 31 pageCount = (recordCount / pageSize) + 1; 32 } 33 for (int i = 2; i >= 1; i--) 34 { 35 if (pageIndex - i >= 1) 36 { 37 htmlLeftPage += string.Format("<a href=\"{0}\">{1}</a>", string.Format(pageLink, (pageIndex - i)), pageIndex - i); 38 } 39 } 40 for (int j = 1; j <= 2; j++) 41 { 42 if (pageIndex + j <= pageCount) 43 { 44 htmlRightPage += string.Format("<a href=\"{0}\">{1}</a>", string.Format(pageLink, (pageIndex + j)), pageIndex + j); 45 } 46 } 47 long prevPage = pageIndex - 1; 48 if (prevPage < 1) { prevPage = 1; } 49 long nextPage = pageIndex + 1; 50 if (nextPage > pageCount) { nextPage = pageCount; } 51 //最后处理 52 if (nextPage < 1) { nextPage = 1; } 53 if (pageCount < 1) { pageCount = 1; } 54 //计算结束 55 string leftHtml = string.Format("<a {0}>{1}</a><a {2}>{3}</a>{4}", pageIndex != 1 ? string.Format("href=\"{0}\"", string.Format(pageLink, 1)) : "", homePageText, pageIndex != prevPage ? string.Format("href=\"{0}\"", string.Format(pageLink, prevPage)) : "", prevPageText, htmlLeftPage); 56 string rightHtml = string.Format("{0}<a {1}>{2}</a><a {3}>{4}</a>", htmlRightPage, pageIndex != nextPage ? string.Format("href=\"{0}\"", string.Format(pageLink, nextPage)) : "", nextPageText, pageIndex != pageCount ? string.Format("href=\"{0}\"", string.Format(pageLink, pageCount)) : "", lastPageText); 57 html = string.Format("<div class=\"pagerbox\">{0}<b>{1}</b>{2}</div><span>{3}</span>", leftHtml, pageIndex, rightHtml, string.Format(displayMsg, pageIndex, pageCount, recordCount)); 58 return html;
分类: Asp.net