asp.net Linq TO Sql 分页方法
程序员文章站
2024-03-08 22:32:52
分页方法 复制代码 代码如下: /// /// /// ///
分页方法
/// <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;
}
}
复制代码 代码如下:
/// <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;
}
}