程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> asp.net Linq TO Sql 分頁方法

asp.net Linq TO Sql 分頁方法

編輯:ASP.NET基礎
分頁方法
復制代碼 代碼如下:
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="replist">控件ID</param>
/// <param name="DataSource">數據源</param>
/// <param name="IndexPage">當前頁</param>
/// <param name="PageSize">每頁數據條數</param>
/// <param name="PageParemart">頁面搜索參數 like &a=a&b=b </param>
/// <returns></returns>
public static string ShowPage<T>(System.Web.UI.WebControls.Repeater replist, IQueryable<T> DataSource, int IndexPage, int PageSize, string PageParemart)
{
string rtnStr = "";
int sourceCount = DataSource.Count();
if (sourceCount == 0)//數據源無數據
{
rtnStr = string.Empty;
}
else
{
int yutemp = sourceCount % PageSize;
int pagecounts = (yutemp == 0) ? (sourceCount / PageSize) : (sourceCount / PageSize + 1);//總頁數
rtnStr = " <div style='width:100%;'><div style=' float:left;'>頁次:" + IndexPage + "頁/" + pagecounts + "頁,共" + sourceCount + "條記錄</div> ";
if (pagecounts == 1) //總共一頁數據
{
replist.DataSource = DataSource;
rtnStr += "[首頁] [上一頁] [下一頁] [尾頁] ";
}
else
{
rtnStr += "<div style=' float:right;'>";
if (IndexPage == 1)//首頁
{
replist.DataSource = DataSource.Take(PageSize);
rtnStr += "[首頁] [上一頁] <a href='?page=" + (IndexPage + 1) + PageParemart + "'>[下一頁]</a> <a href='?page=" + (pagecounts) + PageParemart + "'>[尾頁]</a> ";
}
else
{
replist.DataSource = DataSource.Skip((IndexPage - 1) * PageSize).Take(PageSize);
if (IndexPage == pagecounts)//末頁
{
rtnStr += "<a href='?page=1" + PageParemart + "'>[首頁]</a> <a href='?page=" + (IndexPage - 1) + PageParemart + "'>[上一頁]</a> [下一頁] [尾頁] ";
}
else
{
rtnStr += "<a href='?page=1" + PageParemart + "'>[首頁]</a> <a href='?page=" + (IndexPage - 1) + PageParemart + "'>[上一頁]</a> <a href='?page=" + (IndexPage + 1) + PageParemart + "'>[下一頁]</a> <a href='?page=" + (pagecounts) + PageParemart + "'>[尾頁]</a> ";
}
}
rtnStr += "</div></div>";
}
replist.DataBind();
}
return rtnStr;
}

頁面調用
復制代碼 代碼如下:
private int PageSize = 10;
private int IndexPage = 1;
private string PageParemart = "";
private void Bind()
{
strwhere = "1=1 " + strwhere;
str2 = "1=1 " + str2;
var a = from b in datas.fav_Awards_User select b;
Label2.Text = common.PageFen.ShowPage(replist, a, this.IndexPage, this.PageSize, this.PageParemart);
if (Label2.Text == "")
{
Label1.Visible = true;
}
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved