当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > asp.net Linq TO Sql 分页方法

ASP.NET
.Net中使用com组件后发生System.ArithmeticException异常的解决办法
SQL Server.net 和 OLE DB.net连接数据库的比较
后台更新DataTable行内容的方法
敏捷软件开发(原则,模式与实践)笔记1
确保文本框输入值为数值的代码
XML和数据库之间相互的映射
让你的.NET程序兼容不同版本的Dll文件。
.NET 的数据访问应用程序块(Data Access Application Block)
用控件仅一条指令实现界面换肤和多语言版本(YFSkins)
Microsoft User Interface Process Application Block 研究(3)
分享:处理Excel方法小结
基于ASP.NET实现全球化
.net 里面 protected private 的变量也可以访问(新发现)。
关于C#中{0}和{1}的问题初次在此发贴,问题对你易对我难,求救了
使用C#代码实现增加用户帐号
全世界都在关注-微软重大产品发布
教你做Rational Rose(UML Design)
OLE DB取得数据库的架构信息
VB 从零开始编外挂(三)
XPath序列之四

ASP.NET 中的 asp.net Linq TO Sql 分页方法


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-02-27   浏览: 136 ::
收藏到网摘: n/a

临近春节,手头工作已告一段落,闲来无事写了一个 linq to sql 分页方法。代码若有不妥之处,请各位高手多提宝贵意见。 分页方法
复制代码 代码如下:

/// <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;
}
}