1.下載AspNetPager.dll,並引用到web層
2.在html頁面頂部引入
<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>
3.在html頁面放分頁按鈕處
<webdiyer:AspNetPager ID="AspNetPager1" runat="server"
AlwaysShow="True" FirstPageText="首頁" LastPageText="尾頁" NextPageText="下一頁"
OnPageChanged="AspNetPager1_PageChanged" PrevPageText="上一頁"
PageSize="12" UrlPaging="true" NumericButtonTextFormatString="[{0}]" ShowCustomInfoSection="Left">
</webdiyer:AspNetPager>
4.最後一步,後臺綁定
/// <summary>
/// 綁定repeater數據
/// </summary>
public void RepeaterDataBind()
{
StringBuilder sqlWhere = new StringBuilder();
if (!string.IsNullOrEmpty(Number))
{//維護
sqlWhere.Append(" Del!=0 and ParentNumber='");
sqlWhere.Append(Number + "'");
btn_Return.Visible = true;
}
else
{//默認
sqlWhere.Append(" Del!=0 and ParentNumber='0'");
}
if (Session["where"] != null)
{//查詢
sqlWhere.Append(Session["where"].ToString());
}
rep_Dictionary.DataSource = sys_DictionaryBLL.Instance.GetList(this.AspNetPager1.PageSize, this.AspNetPager1.CurrentPageIndex, sqlWhere.ToString()); //綁定數據
this.rep_Dictionary.DataBind();
this.AspNetPager1.RecordCount = sys_DictionaryBLL.Instance.GetRecordCount(sqlWhere.ToString());//總記錄數
AspNetPager1.CustomInfoHTML = "記錄總數:<b>" + AspNetPager1.RecordCount.ToString() + "</b> ";
AspNetPager1.CustomInfoHTML += "總頁數:<b>" + AspNetPager1.PageCount.ToString() + "</b> ";
AspNetPager1.CustomInfoHTML += "當前頁:<font color='red'><b>" + AspNetPager1.CurrentPageIndex.ToString() + "</b></font>";
}
/// <summary>
/// 分頁
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
RepeaterDataBind();
}
5.DAL層分頁代碼
/// <summary>
/// 分頁獲取數據列表
/// </summary>
public DataSet GetList(int PageSize, int PageIndex, string strWhere)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("select top ");
strSql.Append(PageSize);
strSql.Append(" * from trip_sys_Dictionary where ID not in(select top ");
strSql.Append(PageSize*(PageIndex - 1));
strSql.Append(" ID from trip_sys_Dictionary");
if (!string.IsNullOrEmpty(strWhere))
{
strSql.Append(" where "+strWhere);
strSql.Append(") and "+strWhere);
}
else
{
strSql.Append(")");
}
return DbHelperSQL.Query(strSql.ToString());
}
效果圖如下: