asp.net Linq TO Sql 分頁方法
更新時間:2010年02月11日 14:53:24 作者:
臨近春節(jié),手頭工作已告一段落,閑來無事寫了一個 linq to sql 分頁方法。代碼若有不妥之處,請各位高手多提寶貴意見。
分頁方法
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="replist">控件ID</param>
/// <param name="DataSource">數(shù)據(jù)源</param>
/// <param name="IndexPage">當(dāng)前頁</param>
/// <param name="PageSize">每頁數(shù)據(jù)條數(shù)</param>
/// <param name="PageParemart">頁面搜索參數(shù) 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)//數(shù)據(jù)源無數(shù)據(jù)
{
rtnStr = string.Empty;
}
else
{
int yutemp = sourceCount % PageSize;
int pagecounts = (yutemp == 0) ? (sourceCount / PageSize) : (sourceCount / PageSize + 1);//總頁數(shù)
rtnStr = " <div style='width:100%;'><div style=' float:left;'>頁次:" + IndexPage + "頁/" + pagecounts + "頁,共" + sourceCount + "條記錄</div> ";
if (pagecounts == 1) //總共一頁數(shù)據(jù)
{
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;
}
頁面調(diào)用
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;
}
}
復(fù)制代碼 代碼如下:
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="replist">控件ID</param>
/// <param name="DataSource">數(shù)據(jù)源</param>
/// <param name="IndexPage">當(dāng)前頁</param>
/// <param name="PageSize">每頁數(shù)據(jù)條數(shù)</param>
/// <param name="PageParemart">頁面搜索參數(shù) 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)//數(shù)據(jù)源無數(shù)據(jù)
{
rtnStr = string.Empty;
}
else
{
int yutemp = sourceCount % PageSize;
int pagecounts = (yutemp == 0) ? (sourceCount / PageSize) : (sourceCount / PageSize + 1);//總頁數(shù)
rtnStr = " <div style='width:100%;'><div style=' float:left;'>頁次:" + IndexPage + "頁/" + pagecounts + "頁,共" + sourceCount + "條記錄</div> ";
if (pagecounts == 1) //總共一頁數(shù)據(jù)
{
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;
}
頁面調(diào)用
復(fù)制代碼 代碼如下:
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;
}
}
您可能感興趣的文章:
- asp.net使用LINQ to SQL連接數(shù)據(jù)庫及SQL操作語句用法分析
- asp.net中一個linq分頁實(shí)現(xiàn)代碼
- asp.net中通過ALinq讓Mysql操作變得如此簡單
- asp.net 根據(jù)漢字的拼音首字母搜索數(shù)據(jù)庫(附 LINQ 調(diào)用方法)
- asp.net Linq to Xml學(xué)習(xí)筆記
- asp.net LINQ中數(shù)據(jù)庫連接字符串的問題
- asp.net Linq To Xml上手Descendants、Elements遍歷節(jié)點(diǎn)
- .NET 9 中 LINQ 新增功能實(shí)現(xiàn)過程
相關(guān)文章
詳解ASP.NET Core應(yīng)用中如何記錄和查看日志
本篇文章主要介紹了ASP.NET Core應(yīng)用中如何記錄和查看日志,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2016-12-12ASP與ASP.NET互通COOKIES的一點(diǎn)經(jīng)驗(yàn)
ASP與ASP.NET互通COOKIES的一點(diǎn)經(jīng)驗(yàn)...2006-09-09DropDownList獲取的SelectIndex一直為0的問題
由于初始化判斷出錯導(dǎo)致每次傳到服務(wù)器的時候會初始化一次,這就導(dǎo)致每次獲取DropDownList的SelectIndex的時候只能是02014-06-06