熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> ASP編程 >> 正文

用AspNetPager控件最詳細的分頁實現方法

2022-06-13   來源: ASP編程 

  祝賀一下~好不容易把分頁做出來了之前都是用GridView自帶的分頁版式難看不說還極不優化一次取出所有記錄然後進行假分頁

  現在用aspNetPager控件做出的真分頁就好多了不過還有改進的地方SQL語句如果換成存儲過程效率會更高

  首先在SqlHelpercs(DAL層中的數據庫助手類用於寫可以復用的基本增刪查改方法)中加上以下代碼

  Code

  /**//// <summary>

  /// 獲取分頁數據

  /// </summary>

  /// <param name=sql>sql語句</param>

  /// <param name=currentPage>當前頁</param>

  /// <param name=pagesize>每頁顯示數</param>

  /// <param name=recordcount></param>

  /// <returns></returns>

  public static DataSet GetPage(string sql int currentPage int pagesize out int recordcount)

  {

  openCon();

  sqlDsClear();

  sqlDa = new SqlDataAdapter(sql sqlConn);

  int startRow = (currentPage ) * pagesize;

  sqlDaFill(sqlDs startRow pagesize table);

  recordcount = GetPageRecord(sql);

  closeCon();

  return sqlDs;

  }

  //返回總的記錄數

  public static int GetPageRecord(string sql)

  {

  openCon();

  sql = RegexReplace(sql order by* );

  sql = select count(*) from ( + sql + ) as temp;

  sqlCmd = new SqlCommand(sql sqlConn);

  int recordcount = intParse(sqlCmdExecuteScalar()ToString());

  closeCon();

  return recordcount;

  }

  然後在BLL層新建一個PageManagercs的分頁操作類封裝一下DAL層方法

  Code

  /**//// <summary>

  /// 獲取分頁數據

  /// </summary>

  /// <param name=sql>sql語句</param>

  /// <param name=currentPage>當前頁</param>

  /// <param name=pagesize>每頁顯示數</param>

  /// <param name=recordcount></param>

  /// <returns></returns>

  public static DataSet GetPage(string sql int currentPage int pagesize out int recordcount)

  {

  return SQLHelperGetPage(sql currentPage pagesize out recordcount);

  }

  需分頁的數據綁定處是這樣的

  Code

  <asp:Repeater ID=repNewsList runat=server>

  <ItemTemplate>

  <tr>

  <td align=center><a listaspx?caid=<%# Eval(caId) %>><%# Eval(name) %></a></td>

  <td align=center><%# Eval(createTime) %></td>

  </tr>

  </ItemTemplate>

  </asp:Repeater>

  然後在綁定代碼下方加上分頁控件(當然這個可以隨便放怎麼好看怎麼放)

  Code

  <!分頁控件>

  <div >

  <webdiyer:AspNetPager ID=AspNetPager runat=server AlwaysShow=True UrlPaging=true

  FirstPageText=首頁 LastPageText=末頁 NextPageText=下一頁 NumericButtonCount=

  onpagechanged=AspNetPager_PageChanged PagingButtonSpacing=px NumericButtonTextFormatString=[{}]

  PRevPageText=上一頁 SubmitButtonText=Go TextAfterPageIndexBox=

  TextBeforePageIndexBox=轉到 ShowCustomInfoSection=Left

  CustomInfoHTML=目前是第%CurrentPageIndex%頁 / 總共%PageCount%頁>

  </webdiyer:AspNetPager>

  </div>

  最後在aspxcs中加上數據的分頁綁定方法(這裡的SQL語句要根據列表顯示的需要進行調整)

  Code

  /**//// <summary>

  /// 綁定帶有分頁的新聞列表

  /// </summary>

  public void BindRepeater()

  {

  int caid = intParse(RequestQueryString[caid]);

  string Sql = select * from news where caId= + caid + order by createTime desc;

  int CurrentPage = AspNetPagerCurrentPageIndex;

  int PageSize = AspNetPagerPageSize;

  int RecordCount;

  DataSet ds = PageManagerGetPage(Sql CurrentPage PageSize out RecordCount);

  AspNetPagerRecordCount = RecordCount;

  AspNetPagerCustomInfoHTML += + RecordCount + 條新聞</b>;

  repNewsListDataSource = ds;

  repNewsListDataBind();

  }

  別忘了在page_load調用一下

  protected void Page_Load(object sender EventArgs e)

  {

  if (!PageIsPostBack)

  {

  BindRepeater();

  }

  }

  還有分頁控件的PageChanged事件裡也調用一下

  protected void AspNetPager_PageChanged(object sender EventArgs e)

  {

  BindRepeater();

  }

  最後再補充一個非常漂亮的翻頁樣式清爽超酷型~

  Code

  <style>

  anpager

  {}{

  fontsize:px;

  }

  anpager cpb

  {}{

  background:#FA none repeat scroll ;

  border:px solid #CCCCCC;

  color:#FFFFFF;

  fontweight:bold;

  margin:px px ;

  padding:px px ;

  }

  anpager a

  {}{

  background:#FFFFFF none repeat scroll ;

  border:px solid #CCCCCC;

  color:#FA;

  margin:px px ;

  padding:px px ;

  textdecoration:none

  }

  anpager a:hover

  {}{

  background:#FA none repeat scroll ;

  border:px solid #FA;

  color:#FFFFFF;

  }

  </style>

  然後在AspNetPager中加上以下四個屬性搞定!

  CSSClass=anpager

  CurrentPageButtonClass=cpb

  CustomInfoClass=

  CustomInfoTextAlign=Left


From:http://tw.wingwit.com/Article/program/ASP/201311/21651.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.