当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net中一个linq分页实现代码

asp.net中一个linq分页实现代码

2017年12月12日  | 移动技术网IT编程  | 我要评论

apk 游戏,三支一扶考试,朕与少傅解官袍

linq分页
复制代码 代码如下:

testdatacontext dc = new testdatacontext();
public string getpagenum(gridview gridviewname, int pagesize, iqueryable<test> sql)
{
int page;
if (httpcontext.current.request.querystring["page"] != null)
page = convert.toint32(httpcontext.current.request.querystring["page"]);
else
page = 1;
//var sql = from o in dc.test select o;
int total = sql.count();//总数据量
var sqls = sql.skip((page - 1) * pagesize).take(pagesize);
gridviewname.datasource = sqls;
gridviewname.databind();
int allpage = 0;
int next = 0;
int pre = 0;
int startcount = 0;
int endcount = 0;
string pagestr = "";
if (page < 1) { page = 1; }
//计算总页数
if (pagesize != 0)
{
allpage = (total / pagesize);
allpage = ((total % pagesize) != 0 ? allpage + 1 : allpage);
allpage = (allpage == 0 ? 1 : allpage);
}
next = page + 1;
pre = page - 1;
startcount = (page + 5) > allpage ? allpage - 9 : page - 4;//中间页起始序号
//中间页终止序号
endcount = page < 5 ? 10 : page + 5;
if (startcount < 1) { startcount = 1; } //为了避免输出的时候产生负数,设置如果小于1就从序号1开始
if (allpage < endcount) { endcount = allpage; } //页码+5的可能性就会产生最终输出序号大于总页码,那么就要将其控制在页码数之内
pagestr = "共" + allpage + "页    ";
pagestr += page > 1 ? "<a href=\"" + httpcontext.current.request.currentexecutionfilepath + "?page=1\">首页</a>  <a href=\"" + httpcontext.current.request.currentexecutionfilepath + "?page=" + pre + "\">上一页</a>" : "首页 上一页";
//中间页处理,这个增加时间复杂度,减小空间复杂度
for (int i = startcount; i <= endcount; i++)
{
pagestr += page == i ? "  <font color=\"#ff0000\">" + i + "</font>" : "  <a href=\"" + httpcontext.current.request.currentexecutionfilepath + "?page=" + i + "\">" + i + "</a>";
}
pagestr += page != allpage ? "  <a href=\"" + httpcontext.current.request.currentexecutionfilepath + "?page=" + next + "\">下一页</a>  <a href=\"" + httpcontext.current.request.currentexecutionfilepath + "?page=" + allpage + "\">末页</a>" : " 下一页 末页";
return pagestr;
}

调用 label1.test=getpagenum(控件名称,每页显示条数,linq查询语句)
普通分页
复制代码 代码如下:

public static string getpagenum(datatable ds, datalist datalistname, int pagesize)
{
pageddatasource objpds = new pageddatasource();
objpds.datasource = ds.defaultview;
objpds.allowpaging = true;
int total = ds.rows.count;
objpds.pagesize = pagesize;
int page;
if (httpcontext.current.request.querystring["page"] != null)
page = convert.toint32(httpcontext.current.request.querystring["page"]);
else
page = 1;
objpds.currentpageindex = page - 1;
datalistname.datasource = objpds;
datalistname.databind();
int allpage = 0;
int next = 0;
int pre = 0;
int startcount = 0;
int endcount = 0;
string pagestr = "";
if (page < 1) { page = 1; }
//计算总页数
if (pagesize != 0)
{
allpage = (total / pagesize);
allpage = ((total % pagesize) != 0 ? allpage + 1 : allpage);
allpage = (allpage == 0 ? 1 : allpage);
}
next = page + 1;
pre = page - 1;
startcount = (page + 5) > allpage ? allpage - 9 : page - 4;//中间页起始序号
//中间页终止序号
endcount = page < 5 ? 10 : page + 5;
if (startcount < 1) { startcount = 1; } //为了避免输出的时候产生负数,设置如果小于1就从序号1开始
if (allpage < endcount) { endcount = allpage; } //页码+5的可能性就会产生最终输出序号大于总页码,那么就要将其控制在页码数之内
pagestr = "共" + allpage + "页    ";
pagestr += page > 1 ? "<a href=\"" + httpcontext.current.request.currentexecutionfilepath + "?page=1\">首页</a>  <a href=\"" + httpcontext.current.request.currentexecutionfilepath + "?page=" + pre + "\">上一页</a>" : "首页 上一页";
//中间页处理,这个增加时间复杂度,减小空间复杂度
for (int i = startcount; i <= endcount; i++)
{
pagestr += page == i ? "  <font color=\"#ff0000\">" + i + "</font>" : "  <a href=\"" + httpcontext.current.request.currentexecutionfilepath + "?page=" + i + "\">" + i + "</a>";
}
pagestr += page != allpage ? "  <a href=\"" + httpcontext.current.request.currentexecutionfilepath + "?page=" + next + "\">下一页</a>  <a href=\"" + httpcontext.current.request.currentexecutionfilepath + "?page=" + allpage + "\">末页</a>" : " 下一页 末页";
return pagestr;
}

调用 label1.test=getpagenum(datatable,控件名称,每页显示条数)

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网