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

