using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class Pagination : WebBlockUserControl { public int MaxResults { get { return (ViewState["MaxResults"] == null ? -1 : (int)ViewState["MaxResults"]); } set { ViewState["MaxResults"] = value; } } private int _PageSize = -1; public int PageSize { get { if (_PageSize == -1) { string ps = Configuration.Get("BrowsePageSize"); if (ps == null || ps.Length == 0) _PageSize = 15; else _PageSize = Int32.Parse(ps); } return _PageSize; } } private int _PageIndex = 1; public int PageIndex { get { if (Request["Page"] != null) { if (!Int32.TryParse(Request["Page"], out _PageIndex)) throw new Exception("Please specify an integer page index."); } else _PageIndex = 1; return _PageIndex; } } private int _LowRow = 0; public int LowRow { get { if (_LowRow == 0) _LowRow = ((PageIndex-1) * PageSize) +1; return _LowRow; } } private int _HighRow = 0; public int HighRow { get { if (_HighRow == 0) _HighRow = (PageIndex * PageSize) + 1; return _HighRow; } } protected override void Render(HtmlTextWriter writer) { int TotalPages = Math.Max(1, (int)Math.Ceiling((double)MaxResults / PageSize)); if(PageIndex == -1) { TotalPages = 1; } if (TotalPages > 1) // build out paging { writer.Write("
"); string cleanQueryString = GetCleanQueryString(Request.QueryString.ToString()); if (cleanQueryString.Length > 0) cleanQueryString += "&"; cleanQueryString = "?" + cleanQueryString; // may not be needed, but ill put it in just in case cleanQueryString = Request.Path + cleanQueryString; writer.Write("View All  "); writer.Write("<<  <  "); int startPageIndex; int endPageIndex; if (PageIndex + 2 >= TotalPages) { endPageIndex = Math.Min(TotalPages, PageIndex + 2); startPageIndex = Math.Max(endPageIndex - 4, 1); } else { startPageIndex = Math.Max(1, PageIndex - 2); endPageIndex = Math.Min(startPageIndex + 4, TotalPages); } for (int i = startPageIndex; i <= endPageIndex; i++) { writer.Write(""); if (i == PageIndex) { writer.Write(""); writer.Write(i); writer.Write(""); } else { writer.Write(i); } writer.Write("  "); } writer.Write(">  >>"); writer.Write("
"); } } private string GetCleanQueryString(string q) { // the first step is that we need to remove any pagination info from the query string // example query strings: // Case 1: // Case 2: Search=stuff // Case 3: Search=stuff&Page=10 // Case 4: Search=stuff&Page=10&Color=red // Case 5: Page=10&Search=stuff // Case 6: Page=10 int startIndex = q.IndexOf("Page="); if (startIndex == -1) { // case 1 and 2 // do nothing } else { int endIndex = q.IndexOf('&', startIndex); if (endIndex == -1) { if (startIndex == 0) // case 3 q = q.Remove(startIndex); else // case 6 q = q.Remove(startIndex - 1); } else { if (startIndex == 0) // case 5 q = q.Remove(startIndex, endIndex); else // case 4 q = q.Remove(startIndex - 1, endIndex - startIndex + 1); } } return q; } }