前台有个容器用来存放创建的HtmlAnchor
生成的样式:首页 上一页 12345 下一页 末页
////// /// /// index first page is 0 ///HtmlAnchor GetPageLink(int page) { HtmlAnchor a = new HtmlAnchor(); if (page < 0 || page > ItemCount / PageSize) return null; //a.HRef = string.Format(Request.Path + (Request .QueryString .Count >0?string.Empty : "?")+"PageIndex={0}", page.ToString()); a.HRef =ToUriWithUserDomain(string.Format ("/HealthPostList/{0}.aspx",page.ToString ())); a.InnerText = (page + 1).ToString(); return a; } void PageLinks() { Dictionary pageIDs = new Dictionary (); int pageCount = idList.Length / PageSize; for (int i = 0, c = pageCount; i <= c; i++) { var item = idList.Skip(i * PageSize).Take(PageSize).ToArray (); if (item.Length <= 0) continue; pageIDs.Add(i,item); } idList = pageIDs[PageIndex]; //first page HtmlAnchor firstPage =GetPageLink (0); firstPage .InnerText ="首页"; PageLinkList .Controls .Add (firstPage); HtmlAnchor prePage = GetPageLink((PageIndex - 1) < 0 ? 0 : (PageIndex - 1)); prePage.InnerText = "上一页"; PageLinkList.Controls.Add(prePage); int range = 2; int wholeRange = range * 2 + 1; if (wholeRange > pageCount) { wholeRange = pageCount; range = wholeRange / 2; } List showPageIndex = new List (); showPageIndex.Add(PageIndex); while (showPageIndex.Count() < wholeRange) { showPageIndex.Add(showPageIndex.Max() + 1); showPageIndex.Add(showPageIndex.Min() - 1); if (PageIndex < range) showPageIndex.Add(showPageIndex.Max() + 1); if (PageIndex >= pageCount) showPageIndex.Add(showPageIndex.Min() - 1); showPageIndex = showPageIndex.OrderBy(item => item).ToList (); showPageIndex = showPageIndex.Where(item => item >=0 && item < pageCount).ToList(); } //showPageIndex.OrderBy(item => item); foreach (int i in showPageIndex) { if(i==PageIndex ) { HtmlGenericControl font = new HtmlGenericControl("font"); font.Attributes.Add("style", "font-size:24px"); font.Controls.Add(GetPageLink(PageIndex)); PageLinkList.Controls.Add(font); } else PageLinkList.Controls.Add(GetPageLink(i)); } HtmlAnchor nextPage = GetPageLink((PageIndex + 1) > (pageCount - 1) ? PageIndex : (PageIndex + 1)); nextPage.InnerText = "下一页"; PageLinkList.Controls.Add(nextPage); HtmlAnchor endPage = GetPageLink((pageCount - 1)<0?0:(pageCount -1)); endPage.InnerText = "末页"; PageLinkList.Controls.Add(endPage); }
本文转自today4king博客园博客,原文链接:http://www.cnblogs.com/jinzhao/archive/2009/10/27/1590758.html,如需转载请自行联系原作者