asp.net中一个linq分页实现代码
发布时间:2020-12-15 20:34:46 所属栏目:asp.Net 来源:网络整理
导读:LInq分页 复制代码 代码如下: testDataContext dc = new testDataContext(); public string GetPageNum(GridView GridViewName,int pagesize,IQueryabletest sql) { int page; if (HttpContext.Current.Request.QueryString["page"] != null) page = Convert.
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,控件名称,每页显示条数) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- asp.net-core – .Net Core中的Big-Endian处理
- asp.net-mvc – EF6“没有迁移已应用于目标数据库
- asp.net-mvc – ASP.NET MVC 3 RC和Azure?
- asp.net-mvc – .Net 4.5.1框架的maxRequestLeng
- ASP.NET MVC真正注销表单身份验证
- asp.net-core-mvc – 使用RedirectToAction传递T
- asp.net-mvc-3 – “添加区域”未出现在visual s
- asp.net – Datareader,DataAdapter,Dataset,Dat
- asp.net-core – 加密ASP.Net Core中的连接字符串
- asp.net – 添加context.Response.Headers.Add(“
热点阅读