欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

仿百度的sql分页样式分享

程序员文章站 2022-06-02 08:06:00
...

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

{

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

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

{

startcount = 1;

}

if (allpage

{

endcount = allpage;

}

if (page > allpage)

{

page = allpage;

}

if (allpage>1)

{

pagestr = "

";

pagestr += page > 1 ? "仿百度的sql分页样式分享

" : "上一页";

pagestr += "

";

if (allpage > 10)

{

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

if (page

{

for (int i = 1; i

{

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

}

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

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

}

else

if (page

{

pagestr += " 1...";

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

for (int i = startcount; i

{

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

}

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

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

}

else

{

pagestr += " 1...";

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

for (int i = allpage - 8; i

{

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

}

}

}

else

{

for (int i = 1; i

{

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

}

}

pagestr += "

" : " 下一页";

}

return pagestr;

}

相关标签: sql 分页