当前位置:Gxlcms > mysql > 仿百度的sql分页样式分享

仿百度的sql分页样式分享

时间:2021-07-01 10:21:17 帮助过:20人阅读

分页基本是每个程序都会遇到的问题.前面已经发不过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
//QARequestionBLL就是存放Pager的类

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 = "

";

pagestr += page > 1 ? "

" : "上一页";

pagestr += "

";

if (allpage > 10)

{

startcount = (page + 4) > allpage ? allpage - 10 : page - 4;

if (page < 10)

{

for (int i = 1; i <=10; i++)

{

pagestr += page == i ? "" + i + "" : "" + i + "";

}

//pagestr += "... " + Convert.ToString(allpage - 1) + "";

pagestr += "..." + allpage + "";

}

else

if (page <= allpage - 8)

{

pagestr += " 1...";

//pagestr += " 2...";

for (int i = startcount; i <= startcount + 8; i++)

{

pagestr += page == i ? " " + i + "" : " " + i + "";

}

//pagestr += "... " + Convert.ToString(allpage - 1) + "";

pagestr += "..." + allpage + "";

}

else

{

pagestr += " 1...";

//pagestr += " 2...";

for (int i = allpage - 8; i <= allpage; i++)

{

pagestr += page == i ? " " + i + "" : " " + i + "";

}

}

}

else

{

for (int i = 1; i <= allpage; i++)

{

pagestr += page == i ? " " + i + "" : " " + i + "";

}

}

pagestr += "

" : " 下一页";

}

return pagestr;

}

人气教程排行