asp.net中一個linq分頁實現代碼

來源:互聯網
上載者:User

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 + "頁 &nbsp";
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 + "頁 &nbsp";
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,控制項名稱,每頁顯示條數)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.