- C# code
/// <summary> /// 分页方法 /// </summary> /// <param name="currentUrl">页面地址</param> /// <param name="pageName">分页的参数名</param> /// <param name="recordCount">总条数</param> /// <param name="pageSize">页面每页显示的条数</param> /// <param name="pageCount">页面底部显示多少个分页按钮 如:[第1页] [第2页] [第3页] [第4页] [第5页] 那么就是5</param> /// <param name="pageIndex">当前页</param> /// <returns></returns> public string GetPageBusiness(string currentUrl, string pageName, string condition, int recordCount, int pageSize, int pageCount, int pageIndex) { if (recordCount <= pageSize) { return ""; }; int totalPageCount = (recordCount / pageSize) + ((recordCount % pageSize) > 0 ? 1 : 0); //页码超过最大页时转到最后一页 if (totalPageCount < pageIndex) { Response.Redirect(currentUrl + "?" + pageName + "=" + totalPageCount); } StringBuilder address = new StringBuilder(); int startIndex = (pageIndex - pageCount <= 0) ? 1 : pageIndex - pageCount; int endIndex = (pageIndex + pageCount > totalPageCount) ? totalPageCount : (pageIndex + pageCount); condition = String.IsNullOrEmpty(condition) ? "" : ("&" + condition); if (pageIndex > 1) { address.Append("<a href='" + currentUrl + "?" + condition + "'>首页</a> "); address.Append("<a href='" + currentUrl + "?" + pageName + "=" + (pageIndex - 1) + condition + "'>上一页</a> "); } for (int i = startIndex; i <=endIndex; i++) { if (i == pageIndex) { address.Append("<b>" + i.ToString() + "</b> "); } else { address.Append("<a href='" + currentUrl + "?" + pageName + "=" + i.ToString() + condition + "' title='第"+i.ToString()+"页'>[" + i.ToString() + "]</a>"); } } if (pageIndex < totalPageCount) { address.Append("<a href='" + currentUrl + "?" + pageName + "=" + (pageIndex + 1) + condition + "'>下一页</a> "); } address.Append("<a href='" + currentUrl + "?" + pageName + "=" + totalPageCount + condition + "'>尾页</a>"); return address.ToString(); }
目前的结果:
想实现的结果。
如果当前页是1 那么显示的是1-10
如果是当前页2 显示的也是10.只要当前页没超过10都是显示的1-10
如果是当前页是11 那么显示的是11-20.就是这么一个逻辑。
我开始做其他事情去了。现在算这东西脑袋糊涂了。不清晰了!没办法,不能用控件,只有自己算了。
麻烦各位了
------解决方案--------------------------------------------------------
1、判断当前接收的值、当前值如果大于某个值(10)、则显示下10条记录、一次类推吖!
2、分页前面加个首页、最后一页!
------解决方案--------------------------------------------------------
- C# code
public static string CreatePager(int pageIndex, int pageCount, int totalRecord) { StringBuilder append = new StringBuilder(); append.Append("<div class=\"SelectPager\">"); int step = 5, StartPage = 1, EndPage = pageIndex + step; if (pageIndex > step) { StartPage = pageIndex - step; } if (EndPage > pageCount) { EndPage = pageCount; } else { EndPage = EndPage - 1; } if (pageIndex > StartPage) { append.AppendFormat("<a id=\"first\" href=\"{0}?page={1}{2}\" title=\"首頁\">首頁</a>", System.Web.HttpContext.Current.Request.FilePath, 1); append.AppendFormat("<a id=\"previous\" href=\"{0}?page={1}{2}\" title=\"上一頁\">上一頁</a>", System.Web.HttpContext.Current.Request.FilePath, pageIndex - 1); } for (int i = StartPage; i <= EndPage; i++) { if (pageIndex == i) { append.AppendFormat("<a href=\"javascript:void(0)\" class=\"SelectPagerItem\" > {0} </a>", i); } else { append.AppendFormat("<a href=\"{0}?page={1}{2}\" title=\"第{1}页\" > {1} </a>", System.Web.HttpContext.Current.Request.FilePath, i); } } if (pageIndex < EndPage) { append.AppendFormat("<a href=\"{0}?page={1}{2}\" id=\"next\" title=\"下一頁\">下一頁</a>", System.Web.HttpContext.Current.Request.FilePath, pageIndex + 1); append.AppendFormat("<a href=\"{0}?page={1}{2}\" id=\"last\" title=\"尾页\">尾页</a>", System.Web.HttpContext.Current.Request.FilePath, pageCount); } append.AppendFormat("<span>共<font style=\"color:#E54816\">{0}</font>条数据<font style=\"color:#E54816\">{1}</font>页 当前第<font style=\"color:#E54816\">{2}</font>页</span>", totalRecord, pageCount, pageIndex); append.Append("</div>"); return append.ToString(); }调用 protected string GetPageConnt{ get{return CreatePager(1,100,1000);}}
------解决方案--------------------------------------------------------
- C# code
/// <summary> /// 分页方法 /// </summary> /// <param name="currentUrl">页面地址</param> /// <param name="pageName">分页的参数名</param> /// <param name="recordCount">总条数</param> /// <param name="pageSize">页面每页显示的条数</param> /// <param name="pageCount">页面底部显示多少个分页按钮 如:[第1页] [第2页] [第3页] [第4页] [第5页] 那么就是5</param> /// <param name="pageIndex">当前页</param> /// <returns></returns> public string GetPageBusiness(string currentUrl, string pageName, string condition, int recordCount, int pageSize, int pageCount, int pageIndex) { if (recordCount <= pageSize) { return ""; }; //int totalPageCount = (recordCount / pageSize) + ((recordCount % pageSize) > 0 ? 1 : 0); int totalPageCount = (int)Math.Ceiling(recordCount * 1.0 / pageSize); //页码超过最大页时转到最后一页 if (totalPageCount < pageIndex) { pageIndex = totalPageCount; //Response.Redirect(currentUrl + "?" + pageName + "=" + totalPageCount); } StringBuilder address = new StringBuilder(); pageIndex = pageIndex <= 0 ? 1 : pageIndex; int startIndex = pageIndex % pageCount == 0 ? pageIndex - (pageCount-1) : pageIndex / pageCount * pageCount + 1; int endIndex = pageIndex % pageCount == 0 ? pageIndex : pageIndex + (pageCount - (pageIndex % pageCount)); endIndex = endIndex > totalPageCount ? totalPageCount : endIndex; condition = String.IsNullOrEmpty(condition) ? "" : ("&" + condition); if (pageIndex > 1) { //address.Append("<a href='" + currentUrl + "?" + condition + "'>首页</a> "); address.Append("<a href='" + currentUrl + "?" + pageName + "=1'>首页</a> "); address.Append("<a href='" + currentUrl + "?" + pageName + "=" + (pageIndex - 1) + condition + "'>上一页</a> "); } for (int i = startIndex; i <= endIndex; i++) { if (i == pageIndex) { address.Append("<b>" + i.ToString() + "</b> "); } else { address.Append("<a href='" + currentUrl + "?" + pageName + "=" + i.ToString() + condition + "' title='第" + i.ToString() + "页'>[" + i.ToString() + "]</a>"); } } if (totalPageCount < recordCount) { address.Append("<a href='" + currentUrl + "?" + pageName + "=" + (pageIndex + 1) + condition + "'>下一页</a> "); } address.Append("<a href='" + currentUrl + "?" + pageName + "=" + totalPageCount + condition + "'>尾页</a>"); return address.ToString(); }调用:int index = 1; index = int.TryParse(Request.QueryString["page"], out index) ? index : 0; Response.Write(GetPageBusiness("", "page", "", 888, 10, 10, index));
------解决方案--------------------------------------------------------
存储过程 + aspnetpager不是很好吗