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