protected void Pagination( int totalCount, int pageSize, int currentPage, string url) ... { int count = 0, next = 0, prev = 0, startCount = 0, endCount = 0; if (currentPage < 1) currentPage = 1; if (pageSize != 0) ...{ count = totalCount / pageSize; count = ((totalCount % pageSize) != 0 ? count + 1 : count); count = (count == 0 ? 1 : count); } next = currentPage + 1; prev = currentPage - 1; startCount = (currentPage + 5) > count ? count - 9 : currentPage - 4;//中??起始序? endCount = currentPage < 5 ? 10 : currentPage + 5; //中???止序? if (startCount < 1) ...{ startCount = 1;//?置如果小于1就?序?1?始 } if (count < endCount) ...{ endCount = count;//大于???,那么就要?其控制在???之? } Panel1.Controls.Add(new LiteralControl("共 " + count + " ? ")); if (currentPage > 1) ...{ //pageStr += "<asp:LinkButton ID='ltnFirst' runat='server' PostBackUrl='"+url+"currentPage=1' OnClick='btnPage_Click'>首?</asp:LinkButton> "; //pageStr += "<asp:LinkButton ID='ltnPrev' runat='server' PostBackUrl='" + url + "currentPage="+prev+"' OnClick='btnPage_Click'>上一?</asp:LinkButton>"; LinkButton ltnFirst = new LinkButton(); ltnFirst.ID = "ltnFirst1"; ltnFirst.Text = "首?"; ltnFirst.PostBackUrl = url + "currentPage=1"; ltnFirst.Click += new EventHandler(btnPage_Click); Panel1.Controls.Add(ltnFirst); LinkButton ltnPrev = new LinkButton(); ltnPrev.ID = "ltnPrev1"; ltnPrev.Text = "上一?"; ltnPrev.PostBackUrl = url + "currentPage=" + prev; ltnPrev.Click += new EventHandler(btnPage_Click); Panel1.Controls.Add(ltnPrev); } else ...{ Panel1.Controls.Add(new LiteralControl("首? 上一?")); } for (int i = startCount; i <=endCount; i++) ...{ if (currentPage == i) Panel1.Controls.Add(new LiteralControl(" <font color='red'>" + i + "</font>")); else ...{ //pageStr += " <asp:LinkButton ID='btnPage"+i+"' runat='server' PostBackUrl='"+url+"currentPage="+i+"' OnClick='btnPage_Click'>"+i+"</asp:LinkButton>"; LinkButton hlkPage = new LinkButton(); hlkPage.ID = "hlkPage" + i; hlkPage.Text = i.ToString(); hlkPage.PostBackUrl = url+"currentPage=" + i; hlkPage.Click += new EventHandler(btnPage_Click); Panel1.Controls.Add(hlkPage); } } if (currentPage != count) ...{ //pageStr += "<asp:LinkButton ID='ltnNext' runat='server' PostBackUrl='" + url + "currentPage=" + next + "' OnClick='btnPage_Click'>上一?</asp:LinkButton> "; //pageStr += "<asp:LinkButton ID='ltnLast' runat='server' PostBackUrl='" + url + "currentPage=" + count + "' OnClick='btnPage_Click'>上一?</asp:LinkButton>"; LinkButton ltnNext = new LinkButton(); ltnNext.ID = "ltnNext1"; ltnNext.Text = "下一?"; ltnNext.PostBackUrl = url + "currentPage=" + next; ltnNext.Click += new EventHandler(btnPage_Click); Panel1.Controls.Add(ltnNext); LinkButton ltnLast = new LinkButton(); ltnLast.ID = "ltnLast1"; ltnLast.Text = "尾?"; ltnLast.PostBackUrl = url + "currentPage=" + count; ltnLast.Click += new EventHandler(btnPage_Click); Panel1.Controls.Add(ltnLast); } else ...{ Panel1.Controls.Add(new LiteralControl("下一? 尾?")); } }