本文介绍了一个ASP.NET下的分页方法,这个分页是仿百度的分页效果。具体代码如下
本文介绍了一个ASP.NET下的分页方法,这个分页是仿百度的分页效果。具体代码如下:
首先创建一个类:
//仿百度数字分页
public static void BuildPages(int ToatalCountRecord, int PageItem, int CurrentPage, HtmlGenericControl PageInfo, string strWhere) {
int Step = 5;//偏移量
int LeftNum = 0;//左界限
int RightNum = 0;//右界限
string PageUrl = HttpContext.Current.Request.FilePath;
int PageCount = (int)Math.Ceiling((double)(ToatalCountRecord) / PageItem);
if (CurrentPage - Step < 1)
{
LeftNum = 1;
}
else
{
LeftNum = CurrentPage - Step;
}
if (CurrentPage + Step > PageCount)
{
RightNum = PageCount;
}
else
{
RightNum = CurrentPage + Step;
}
string OutPut = "";
if (strWhere != null)
strWhere = "&" + strWhere;
OutPut += "<a href="" + PageUrl + "?page=1" + strWhere + "" mce_href="" + PageUrl + "?page=1" + strWhere + "">" + "首页" + "</a>";
if (CurrentPage > 1)
{
OutPut += " <a href="" + PageUrl + "?page=" + (CurrentPage - 1) + strWhere + "" mce_href="" + PageUrl + "?page=" + (CurrentPage - 1) + strWhere + "">" + "上一页" + " </a>";
}
if (CurrentPage == 1)
{
OutPut += " 上一页 ";
}
for (int i = LeftNum; i <= RightNum; i++)
{
if (i == CurrentPage)
{
OutPut += " " + "[" + i.ToString() + "]" + "";
}
else
{
OutPut += " <a href="" + PageUrl + "?page=" + i.ToString() + strWhere + "" mce_href="" + PageUrl + "?page=" + i.ToString() + strWhere + "">" + " " + "[" + i.ToString() + "]" + " " + " </a>";
}
}
if (CurrentPage < PageCount)
{
OutPut += " <a href="" + PageUrl + "?page=" + (CurrentPage + 1) + strWhere + "" mce_href="" + PageUrl + "?page=" + (CurrentPage + 1) + strWhere + "">" + "下一页" + " </a>";
}
if (CurrentPage == PageCount)
{
OutPut += " 下一页 ";
}
int last;
if (ToatalCountRecord % PageItem == 0)
last = ToatalCountRecord / PageItem;
else
last = ToatalCountRecord / PageItem + 1;
OutPut += "<a href="" + PageUrl + "?page=" + last + strWhere + "" mce_href="" + PageUrl + "?page=" + last + strWhere + "">" + "末页" + "</a>";
PageInfo.InnerHtml = OutPut;
}
BLL层获取数据:
/// 获得数据列表 BLL层
public DataSet GetList(string strWhere, int startRecord, int MaxRecord)
{
return dal.GetList(strWhere, startRecord, MaxRecord);
}
//获得数据列表,DAL层
public DataSet GetList(string strWhere, int startRecord, int MaxRecord)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("select * ");
strSql.Append(" FROM QQ ");
if (strWhere.Trim() != "")
{
strSql.Append(" where " + strWhere);
}
return DbHelperOleDb.Query(strSql.ToString(), startRecord, MaxRecord);
}
//DBUtility层 DbHelperOleDb.cs
public static DataSet Query(string SQLString, int startRecord, int MaxRecord)
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
DataSet ds = new DataSet();
try
{
connection.Open();
OleDbDataAdapter command = new OleDbDataAdapter(SQLString, connection);
command.Fill(ds, startRecord, MaxRecord, "ds");
}
catch (System.Data.OleDb.OleDbException ex)
{
throw new Exception(ex.Message);
}
return ds;
}
}
调用分页时的前台代码:
<div id="PageInfo" runat="server" class="text" style=" vertical-align:baseline;" mce_style=" vertical-align:baseline;"> </div>
调用分页的后台代码:
//分页后台代码 fenye.aspx.cs
public partial class fenye : System.Web.UI.Page
{
Maticsoft.BLL.QQ bq = new Maticsoft.BLL.QQ();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
RBind();
}
private void RBind()
{
int ToatalCountRecord;//总记录数
int PageItem = 2;//每页显示的条数
int CurrentPage = 1;//当前页数
if (Request.QueryString["page"] != null)
CurrentPage = int.Parse(Request.QueryString["page"].ToString());
int startRecord = (CurrentPage - 1) * PageItem;
DataSet ds = new DataSet();
ds = bq.GetList("", startRecord, PageItem);
this.Repeater1.DataSource = ds;
this.Repeater1.DataBind();
ToatalCountRecord = bq.Rows("");
JScript.BuildPages(ToatalCountRecord, PageItem, CurrentPage, PageInfo, null);
}
}