仿百度的sql分页样式分享
代码如下 | 复制代码 |
protected DataTable dtNews ; const int PAGE_SIZE = 7; protected string pagestr; protected string pagestring; protected void Page_Load(object sender, EventArgs e) { string urlParam = Request["p"] ?? string.Empty; int pageIndex; int.TryParse(urlParam.Trim(), out pageIndex); if (pageIndex < 1) { pageIndex = 1; } |
//这里就是调用sql分页通用存储过程的方法,我把SelectPager()方法贴出来
代码如下 | 复制代码 |
View Code this.dtNews = QARequestionBLL.SelectPager(pcategory, this._CurPageIndex, _pageSize, "QDatetime", "desc", out doCount); pagestring = GetPageStr(doCount, PAGE_SIZE, pageIndex); } |
//声明一下.这里面的URLTO,是我用了重定向的方法.把他提取出来一个类了,所以href里面的连接其实就是地址后面加
代码如下 | 复制代码 |
pageindex.(要下班了,所以就不正了,回家有事.嘿嘿....) //总共多少也,每页多少条,第几页 public string GetPageStr(int total, int per, int page) { int allpage = 0; int next = 0; int pre = 0; int startcount = 0; int endcount = 0;
if (page < 1) { page = 1; } if (per != 0) { allpage = (total / per); allpage = ((total % per) != 0 ? allpage + 1 : allpage); allpage = (allpage == 0 ? 1 : allpage); } next = page + 1; pre = page - 1; if (startcount < 1) { startcount = 1; } if (allpage < endcount) { endcount = allpage; } if (page > allpage) { page = allpage; } if (allpage>1) { pagestr = "<div><span>"; pagestr += page > 1 ? "<a href=" +URLto.getNewsList(pre) + "><img src='images/default_cms/Video_list/prev.gif' alt=''/></a></span></div>" : "<font style='display:none'>上一页</font></div>"; pagestr += "<div>"; if (allpage > 10) { startcount = (page + 4) > allpage ? allpage - 10 : page - 4; if (page < 10) {
for (int i = 1; i <=10; i++) { pagestr += page == i ? "<span class='current'>" + i + "</span>" : "<a href=" +URLto.getNewsList(i) + ">" + i + "</a>"; } //pagestr += "... <a href=" + Facebook.GameEN.CMS.Website.Util.URLto.getNewsList(allpage - 1) + ">" + Convert.ToString(allpage - 1) + "</a>"; pagestr += "<span class='current'>...</span><a href=" +URLto.getNewsList(allpage) + ">" + allpage + "</a>"; } else if (page <= allpage - 8) { pagestr += " <a href=" + URLto.getNewsList(1) + ">1</a><span >...</span>"; //pagestr += " <a href=" + Facebook.GameEN.CMS.Website.Util.URLto.getNewsList(2) + ">2</a>..."; for (int i = startcount; i <= startcount + 8; i++) { pagestr += page == i ? " <span>" + i + "</span>" : " <a href=" +URLto.getNewsList(i) + ">" + i + "</a>"; } //pagestr += "... <a href=" + Facebook.GameEN.CMS.Website.Util.URLto.getNewsList(allpage - 1) + ">" + Convert.ToString(allpage - 1) + "</a>"; pagestr += "<span class='current'>...</span><a href=" +URLto.getNewsList(allpage) + ">" + allpage + "</a>"; } else { pagestr += " <a href=" + Facebook.GameEN.CMS.Website.Util.URLto.getNewsList(1) + ">1</a><span class='current'>...</span>"; //pagestr += " <a href=" + Facebook.GameEN.CMS.Website.Util.URLto.getNewsList(2) + ">2</a>..."; for (int i = allpage - 8; i <= allpage; i++) { pagestr += page == i ? " <span>" + i + "</span>" : " <a href=" +URLto.getNewsList(i) + ">" + i + "</a>"; } }
} else { for (int i = 1; i <= allpage; i++) { pagestr += page == i ? " <span class='current'>" + i + "</span>" : " <a href=" +URLto.getNewsList(i) + ">" + i + "</a>"; } } pagestr += "</div><div class='next'>"; pagestr += page != allpage ? " <span><a href=" +URLto.getNewsList(next) + "><img src='images/default_cms/Video_list/next.gif' alt='' /></a></span></div>" : " <font style='display:none'>下一页</font></div>"; } return pagestr; } |
补充:数据库,Mssql