分页
代码如下:
public static string GetPageControl(int CurrentPage, int TotalPage, string Url) { int intStartPage = 0; int intEndPage = 0; StringBuilder strbControlHTML = new StringBuilder(); //如果当前页不是第一页,显示上一页 if (CurrentPage != 1) { strbControlHTML.Append("<a href=\"" + string.Format(Url, CurrentPage - 1) + "\">上一页 </a>"); } //如果当前页小于5或总页码小于等于6 if (CurrentPage < 5 || TotalPage <= 6) { intEndPage = (TotalPage < 6) ? TotalPage - 1 : 5; if (intEndPage <= 1) intEndPage = -1; for (int i = 0; i <= intEndPage; i++) { if (CurrentPage == i + 1) { strbControlHTML.Append("<a href=\"" + string.Format(Url, i + 1) + "\" style=\"font-weight:bold;\">" + (i + 1) + "</a>"); } else { strbControlHTML.Append("<a href=\"" + string.Format(Url, i + 1) + "\">" + (i + 1) + "</a>"); } } if (TotalPage > 6 ) strbControlHTML.Append("…<a href=\"" + string.Format(Url, TotalPage) + "\">" + TotalPage + "</a>"); } else if (CurrentPage < TotalPage - 4) { strbControlHTML.Append("<a href=\"" + string.Format(Url, 1) + "\">" + 1 + "</a>…"); intStartPage = CurrentPage - 4; if (intStartPage <= 0) intStartPage = 1; for (int i = intStartPage; i <= CurrentPage + 2; i++) { if (CurrentPage == i + 1) { strbControlHTML.Append("<a href=\"" + string.Format(Url, i + 1) + "\" style=\"font-weight:bold;\">" + (i + 1) + "</a>"); } else { strbControlHTML.Append("<a href=\"" + string.Format(Url, i + 1) + "\">" + (i + 1) + "</a>"); } } strbControlHTML.Append("…<a href=\"" + string.Format(Url, TotalPage) + "\">" + TotalPage + "</a>"); } else { strbControlHTML.Append("<a href=\"" + string.Format(Url, 1) + "\">" + 1 + "</a>…"); intStartPage = CurrentPage - 4; if (intStartPage <= 0) intStartPage = 1; for (int i = intStartPage; i < TotalPage; i++) { if (CurrentPage == i + 1) { strbControlHTML.Append("<a href=\"" + string.Format(Url, i + 1) + "\" style=\"font-weight:bold;\">" + (i + 1) + "</a>"); } else { strbControlHTML.Append("<a href=\"" + string.Format(Url, i + 1) + "\">" + (i + 1) + "</a>"); } } } //如果不是最后一页且总页数大于0显示 下一页 if (CurrentPage != TotalPage && TotalPage > 0) strbControlHTML.Append("<a href=\"" + string.Format(Url, CurrentPage + 1) + "\"> 下一页</a>"); return strbControlHTML.ToString(); }
效果图: