asp.net中一個(gè)linq分頁(yè)實(shí)現(xiàn)代碼
更新時(shí)間:2011年12月20日 11:57:50 作者:
asp.net中一個(gè)linq分頁(yè)實(shí)現(xiàn)代碼,需要的朋友可以參考下。
LInq分頁(yè)
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; }
//計(jì)算總頁(yè)數(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;//中間頁(yè)起始序號(hào)
//中間頁(yè)終止序號(hào)
endcount = page < 5 ? 10 : page + 5;
if (startcount < 1) { startcount = 1; } //為了避免輸出的時(shí)候產(chǎn)生負(fù)數(shù),設(shè)置如果小于1就從序號(hào)1開(kāi)始
if (allpage < endcount) { endcount = allpage; } //頁(yè)碼+5的可能性就會(huì)產(chǎn)生最終輸出序號(hào)大于總頁(yè)碼,那么就要將其控制在頁(yè)碼數(shù)之內(nèi)
pagestr = "共" + allpage + "頁(yè)  ";
pagestr += page > 1 ? "<a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=1\">首頁(yè)</a> <a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + pre + "\">上一頁(yè)</a>" : "首頁(yè) 上一頁(yè)";
//中間頁(yè)處理,這個(gè)增加時(shí)間復(fù)雜度,減小空間復(fù)雜度
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 + "\">下一頁(yè)</a> <a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + allpage + "\">末頁(yè)</a>" : " 下一頁(yè) 末頁(yè)";
return pagestr;
}
調(diào)用 label1.Test=GetPageNum(控件名稱,每頁(yè)顯示條數(shù),linq查詢語(yǔ)句)
普通分頁(yè)
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; }
//計(jì)算總頁(yè)數(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;//中間頁(yè)起始序號(hào)
//中間頁(yè)終止序號(hào)
endcount = page < 5 ? 10 : page + 5;
if (startcount < 1) { startcount = 1; } //為了避免輸出的時(shí)候產(chǎn)生負(fù)數(shù),設(shè)置如果小于1就從序號(hào)1開(kāi)始
if (allpage < endcount) { endcount = allpage; } //頁(yè)碼+5的可能性就會(huì)產(chǎn)生最終輸出序號(hào)大于總頁(yè)碼,那么就要將其控制在頁(yè)碼數(shù)之內(nèi)
pagestr = "共" + allpage + "頁(yè)  ";
pagestr += page > 1 ? "<a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=1\">首頁(yè)</a> <a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + pre + "\">上一頁(yè)</a>" : "首頁(yè) 上一頁(yè)";
//中間頁(yè)處理,這個(gè)增加時(shí)間復(fù)雜度,減小空間復(fù)雜度
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 + "\">下一頁(yè)</a> <a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + allpage + "\">末頁(yè)</a>" : " 下一頁(yè) 末頁(yè)";
return pagestr;
}
調(diào)用 label1.Test=GetPageNum(datatable,控件名稱,每頁(yè)顯示條數(shù))
復(fù)制代碼 代碼如下:
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; }
//計(jì)算總頁(yè)數(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;//中間頁(yè)起始序號(hào)
//中間頁(yè)終止序號(hào)
endcount = page < 5 ? 10 : page + 5;
if (startcount < 1) { startcount = 1; } //為了避免輸出的時(shí)候產(chǎn)生負(fù)數(shù),設(shè)置如果小于1就從序號(hào)1開(kāi)始
if (allpage < endcount) { endcount = allpage; } //頁(yè)碼+5的可能性就會(huì)產(chǎn)生最終輸出序號(hào)大于總頁(yè)碼,那么就要將其控制在頁(yè)碼數(shù)之內(nèi)
pagestr = "共" + allpage + "頁(yè)  ";
pagestr += page > 1 ? "<a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=1\">首頁(yè)</a> <a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + pre + "\">上一頁(yè)</a>" : "首頁(yè) 上一頁(yè)";
//中間頁(yè)處理,這個(gè)增加時(shí)間復(fù)雜度,減小空間復(fù)雜度
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 + "\">下一頁(yè)</a> <a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + allpage + "\">末頁(yè)</a>" : " 下一頁(yè) 末頁(yè)";
return pagestr;
}
調(diào)用 label1.Test=GetPageNum(控件名稱,每頁(yè)顯示條數(shù),linq查詢語(yǔ)句)
普通分頁(yè)
復(fù)制代碼 代碼如下:
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; }
//計(jì)算總頁(yè)數(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;//中間頁(yè)起始序號(hào)
//中間頁(yè)終止序號(hào)
endcount = page < 5 ? 10 : page + 5;
if (startcount < 1) { startcount = 1; } //為了避免輸出的時(shí)候產(chǎn)生負(fù)數(shù),設(shè)置如果小于1就從序號(hào)1開(kāi)始
if (allpage < endcount) { endcount = allpage; } //頁(yè)碼+5的可能性就會(huì)產(chǎn)生最終輸出序號(hào)大于總頁(yè)碼,那么就要將其控制在頁(yè)碼數(shù)之內(nèi)
pagestr = "共" + allpage + "頁(yè)  ";
pagestr += page > 1 ? "<a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=1\">首頁(yè)</a> <a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + pre + "\">上一頁(yè)</a>" : "首頁(yè) 上一頁(yè)";
//中間頁(yè)處理,這個(gè)增加時(shí)間復(fù)雜度,減小空間復(fù)雜度
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 + "\">下一頁(yè)</a> <a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + allpage + "\">末頁(yè)</a>" : " 下一頁(yè) 末頁(yè)";
return pagestr;
}
調(diào)用 label1.Test=GetPageNum(datatable,控件名稱,每頁(yè)顯示條數(shù))
您可能感興趣的文章:
- asp.net使用LINQ to SQL連接數(shù)據(jù)庫(kù)及SQL操作語(yǔ)句用法分析
- asp.net中通過(guò)ALinq讓Mysql操作變得如此簡(jiǎn)單
- asp.net 根據(jù)漢字的拼音首字母搜索數(shù)據(jù)庫(kù)(附 LINQ 調(diào)用方法)
- asp.net Linq to Xml學(xué)習(xí)筆記
- asp.net LINQ中數(shù)據(jù)庫(kù)連接字符串的問(wèn)題
- asp.net Linq TO Sql 分頁(yè)方法
- asp.net Linq To Xml上手Descendants、Elements遍歷節(jié)點(diǎn)
- .NET 9 中 LINQ 新增功能實(shí)現(xiàn)過(guò)程
相關(guān)文章
通過(guò)Windows Visual Studio遠(yuǎn)程調(diào)試WSL2中的.NET Core Linux應(yīng)用程序的方法
這篇文章主要介紹了通過(guò)Windows Visual Studio遠(yuǎn)程調(diào)試WSL2中的.NET Core Linux應(yīng)用程序的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
ASP.NET Core 3框架揭秘之 異步線程無(wú)法使用IServiceProvider問(wèn)題
這篇文章主要介紹了ASP.NET Core 3框架揭秘之異步線程無(wú)法使用IServiceProvider問(wèn)題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
ASP.Net MVC+Data Table實(shí)現(xiàn)分頁(yè)+排序功能的方法
這篇文章主要介紹了ASP.Net MVC+Data Table實(shí)現(xiàn)分頁(yè)+排序功能的方法,結(jié)合實(shí)例形式分析了asp.net基于mvc架構(gòu)實(shí)現(xiàn)的數(shù)據(jù)查詢、排序、分頁(yè)顯示等相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
asp.net core集成kindeditor實(shí)現(xiàn)圖片上傳功能
這篇文章主要為大家詳細(xì)介紹了asp.net core集成kindeditor實(shí)現(xiàn)圖片上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
AspNetCore&MassTransit?Courier實(shí)現(xiàn)分布式事務(wù)的詳細(xì)過(guò)程
MassTransit?Courier是一種用于創(chuàng)建和執(zhí)行帶有故障補(bǔ)償?shù)姆植际绞聞?wù)的機(jī)制,它可以用于滿足本地事務(wù)的需求,也可以在分布式系統(tǒng)中實(shí)現(xiàn)分布式事務(wù),這篇文章主要介紹了AspNetCore&MassTransit?Courier實(shí)現(xiàn)分布式事務(wù),需要的朋友可以參考下2022-10-10
asp.net 刪除MFC單文檔默認(rèn)菜單欄的兩種方法
新建一個(gè)MFC單文檔程序,默認(rèn)都有四個(gè)菜單欄:文件、編輯、視圖和幫助。怎么把這四個(gè)菜單欄刪除掉呢?2010-03-03

