ASP.NET 分页导航条
protected static string Paging(int pageIndex, int pageCount) { string url = HttpContext.Current.Request.RawUrl; if (url.IndexOf("?") != -1) { if (url.LastIndexOf("?page") != -1) { url = url.Substring(0, url.LastIndexOf('?')); url += "?"; } else url += "&"; } else { url += "?"; } pageIndex = TypeConverter.StrToInt(HttpContext.Current.Request.QueryString["page"].ToString()); int start = 1; int end = 5; int prev = 0; int next = 0; StringBuilder html = new StringBuilder(); if (pageIndex < 1) //页码 { pageIndex = 1; } if (pageIndex > pageCount) //总页数 { pageIndex = pageCount; } next = pageIndex + 1; prev = pageIndex - 1; if (pageIndex > 3) { start = pageIndex - 2; end = pageIndex + 2; } if (start < 1) { start = 1; } //为了避免输出的时候产生负数,设置如果小于就从序号开始 if (pageCount < end) { end = pageCount; } if (pageIndex > 1) { html.AppendLine("<a href=\"" + url + "page=1\">首页</a>"); html.AppendLine("<a href=\"" + url + "page=" + prev + "\">上一页</a>"); } for (int i = start; i <= end; i++) { if (pageIndex == i) html.AppendLine("<span><a href=\"" + url + "page=" + i.ToString() + "\">" + i.ToString() + "</a></span>"); else html.AppendLine("<a href=\"" + url + "page=" + i.ToString() + "\">" + i.ToString() + "</a>"); } if (pageIndex != pageCount) { html.AppendLine("<a href=\"" + url + "page=" + next + "\">下一页</a>"); html.AppendLine("<a href=\"" + url + "page=" + pageCount + "\">尾页</a>"); } return html.ToString(); }