首页 > 编程 > .NET > 正文

asp.net Linq TO Sql 分页方法

2024-07-10 13:26:43
字体:
来源:转载
供稿:网友
分页方法

复制代码 代码如下:


/// <summary>
///
/// </summary>
/// <typeparam></typeparam>
/// <param>控件ID</param>
/// <param>数据源</param>
/// <param>当前页</param>
/// <param>每页数据条数</param>
/// <param>页面搜索参数 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><div>页次:" + IndexPage + "页/" + pagecounts + "页,共" + sourceCount + "条记录</div> ";
if (pagecounts == 1) //总共一页数据
{
replist.DataSource = DataSource;
rtnStr += "[首页] [上一页] [下一页] [尾页] ";
}
else
{
rtnStr += "<div>";
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;
}
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表