asp.net中一個linq分頁實現(xiàn)代碼
更新時間:2011年12月20日 11:57:50 作者:
asp.net中一個linq分頁實現(xiàn)代碼,需要的朋友可以參考下。
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();//總數(shù)據(jù)量
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; }
//計算總頁數(shù)
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; } //為了避免輸出的時候產(chǎn)生負數(shù),設置如果小于1就從序號1開始
if (allpage < endcount) { endcount = allpage; } //頁碼+5的可能性就會產(chǎn)生最終輸出序號大于總頁碼,那么就要將其控制在頁碼數(shù)之內(nèi)
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;
}
調(diào)用 label1.Test=GetPageNum(控件名稱,每頁顯示條數(shù),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; }
//計算總頁數(shù)
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; } //為了避免輸出的時候產(chǎn)生負數(shù),設置如果小于1就從序號1開始
if (allpage < endcount) { endcount = allpage; } //頁碼+5的可能性就會產(chǎn)生最終輸出序號大于總頁碼,那么就要將其控制在頁碼數(shù)之內(nèi)
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;
}
調(diào)用 label1.Test=GetPageNum(datatable,控件名稱,每頁顯示條數(shù))
復制代碼 代碼如下:
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();//總數(shù)據(jù)量
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; }
//計算總頁數(shù)
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; } //為了避免輸出的時候產(chǎn)生負數(shù),設置如果小于1就從序號1開始
if (allpage < endcount) { endcount = allpage; } //頁碼+5的可能性就會產(chǎn)生最終輸出序號大于總頁碼,那么就要將其控制在頁碼數(shù)之內(nèi)
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;
}
調(diào)用 label1.Test=GetPageNum(控件名稱,每頁顯示條數(shù),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; }
//計算總頁數(shù)
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; } //為了避免輸出的時候產(chǎn)生負數(shù),設置如果小于1就從序號1開始
if (allpage < endcount) { endcount = allpage; } //頁碼+5的可能性就會產(chǎn)生最終輸出序號大于總頁碼,那么就要將其控制在頁碼數(shù)之內(nèi)
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;
}
調(diào)用 label1.Test=GetPageNum(datatable,控件名稱,每頁顯示條數(shù))
相關文章
實現(xiàn)Asp與Asp.Net共享Session的方法
這篇文章主要介紹了實現(xiàn)Asp與Asp.Net共享Session的方法,需要的朋友可以參考下2014-08-08關于服務器或虛擬主機不支持 AjaxPro 的問題終極解決方法
asp.net的網(wǎng)站,訪問時提示不支持 AjaxPro,那就因為誤刪的映射導致,可以通過下面的方法解決2012-03-03ASP.NET實現(xiàn)可以縮放和旋轉的圖片預覽頁效果
本文詳細介紹了如何在ASP.NET?WebForms中實現(xiàn)一個功能豐富的圖片預覽頁面,通過結合HTML、CSS和JavaScript,用戶可以方便地對圖片進行放大、縮小以及旋轉操作,感興趣的朋友跟隨小編一起看看吧2024-08-08Asp.net在ashx文件中處理Session問題解決方法
Asp.net在ashx文件中處理Session問題解決方法,需要的朋友可以參考一下2013-05-05ASP.NET MVC5網(wǎng)站開發(fā)修改及刪除文章(十)
這篇文章主要為大家詳細介紹了ASP.NET MVC5網(wǎng)站開發(fā)修改及刪除文章,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2015-09-09