asp.net Linq TO Sql 分頁方法

來源:互聯網
上載者:User

分頁方法 複製代碼 代碼如下:/// <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;
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.