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

Repeater和DataList分頁

2013-11-13 10:13:34  來源: .NET編程 

  因為項目需要要用到DataList和Repeater的分頁但這兩種加載數據的控件本身並不自帶分頁功能要控制查詢語句來進行分頁但是語句過於復雜或是條件太多語句很難寫但用PagedDataSource進行分頁就簡單多了代碼分配清晰容易理解

  下面實例是用PagedDataSource類實現DataList控件的數據分頁也可用於Repeater

  頁面後台代碼

  using System;

  using SystemData;

  using SystemConfiguration;

  using SystemCollections;

  using SystemWeb;

  using SystemWebSecurity;

  using SystemWebUI;

  using SystemWebUIWebControls;

  using SystemWebUIWebControlsWebParts;

  using SystemWebUIHtmlControls;

  using SystemData;

  using SystemDataSqlClient;

  public partial class Demo : SystemWebUIPage

  {

  protected SqlConnection conn;  //添加數據庫的操作對象

  protected SqlDataAdapter da;

  protected DataSet ds;

  protected SqlCommand comm;

  protected void Page_Load(object sender EventArgs e)

  {

  getArticle();

  }

  private void getArticle()      //取得Article數據

  {

  conn = new SqlConnection(server=;database=ObtainEmployment;user id=sa;password=;);//取連接字符串建立連接

  da = new SqlDataAdapter();

  daSelectCommand = new SqlCommand(SELECT top   * FROM db_Article where  checkup= ORDER BY intime DESC conn);

  ds = new DataSet();

  try

  {

  connOpen();

  daFill(ds Article);

  connClose();

  }

  catch (SqlException e)

  {

  ResponseWrite(eToString());

  }

  int cup = ConvertToInt(thislb_CurrentPageText);  //當前頁數初始化為地

  PagedDataSource ps = new PagedDataSource();

  psDataSource = dsTables[Article]DefaultView;

  psAllowPaging = true;

  psPageSize = ;     //每頁顯示的數據的行數

  psCurrentPageIndex = cup ;

  lb_countText = psDataSourceCountToString(); //獲取記錄總數

  lb_pageText = psPageCountToString(); //獲取總頁數

  if (!IsPostBack)

  {

  for (int i = ; i < psPageCount + ; i++)

  {

  thisDropDownListItemsAdd(iToString());

  }

  LinkUpEnabled = true;

  LinkDownEnabled = true;

  }

  try

  {

  DropDownListSelectedItemText = cupToString();

  DataListDataSource = ps;

  DataListDataBind();

  }

  catch (Exception ex)

  {

  throw ex;

  }

  }

  protected void LinkDown_Click(object sender EventArgs e) //下一頁按鈕代碼

  {

  try

  {

  lb_CurrentPageText = ConvertToString(ConvertToInt(lb_CurrentPageText) + );

  DropDownListSelectedValue = lb_CurrentPageText;

  getArticle();

  }

  catch (Exception ex)

  {

  ResponseWrite(<script language=javascript> + alert(\已經是最後一頁\) + </script>);

  lb_CurrentPageText = ;

  getArticle();

  }

  }

  protected void DropDownList_SelectedIndexChanged(object sender EventArgs e) //跳轉到指定頁代碼

  {

  int page =ConvertToInt((DropDownListSelectedItemValue));

  lb_CurrentPageText = pageToString();

  getArticle();

  }

  protected void LinkUp_Click(object sender EventArgs e)  //上一頁按鈕代碼

  {

  try

  {

  if (ConvertToInt(lb_CurrentPageText) > )

  {

  lb_CurrentPageText = ConvertToString(ConvertToInt(lb_CurrentPageText) );

  DropDownListSelectedValue = lb_CurrentPageText;

  getArticle();

  }

  else

  {

  ResponseWrite(<script language=javascript> + alert(\已經是第一頁\) + </script>);

  }

  }

  catch (Exception ex)

  {

  ResponseWrite(<script language=javascript> + alert(\已經是第一頁\) + </script>);

  }

  }

  protected void LinkFirst_Click(object sender EventArgs e)  //跳到第一頁代碼

  {

  if (lb_CurrentPageText != )

  lb_CurrentPageText = ;

  else

  {

  ResponseWrite(<script language=javascript> + alert(\已經是第一頁\) + </script>);

  }

  getArticle();

  }

  protected void LinkLast_Click(object sender EventArgs e)  //跳到最後一頁代碼

  {

  if (lb_CurrentPageTextToString() !=lb_pageTextToString())

  lb_CurrentPageText = lb_pageTextToString();

  else

  {

  ResponseWrite(<script language=javascript> + alert(\已經是最後一頁\) + </script>);

  }

  getArticle();

  }

  }

  頁面前台代碼

  <%@ Page Language=C# CodeFile=Demoaspxcs Inherits=Demo %>

  <!DOCTYPE html PUBLIC //WC//DTD XHTML Transitional//EN transitionaldtd>

  <html xmlns= >

  <head runat=server>

  <title>無標題頁</title>

  </head>

  <body>

  <form id=form runat=server>

  <div>

  <asp:DataList ID=DataList runat=server>

  <ItemTemplate>

  <asp:Label ID=lbNwes runat=server Text=<%#eval_r(title)%>></asp:Label>

  &nbsp;

  <asp:Label ID=lbTime runat=server Text=<%#eval_r(intime)%>></asp:Label>

  </ItemTemplate>

  </asp:DataList></div>

  <br />

  共<asp:Label ID=lb_count runat=server Text=Label></asp:Label>條記錄

  &nbsp; &nbsp; &nbsp; 共<asp:Label ID=lb_page runat=server Text=Label></asp:Label>頁 &nbsp;

  &nbsp; 當前第<asp:Label ID=lb_CurrentPage runat=server Text=></asp:Label>頁&nbsp; &nbsp;

  &nbsp; &nbsp;

  <asp:LinkButton ID=LinkFirst runat=server OnClick=LinkFirst_Click>第一頁</asp:LinkButton>

  &nbsp; &nbsp;

  <asp:LinkButton ID=LinkUp runat=server OnClick=LinkUp_Click>上一頁</asp:LinkButton>

  &nbsp; &nbsp;<asp:LinkButton ID=LinkDown runat=server OnClick=LinkDown_Click>下一頁</asp:LinkButton>

  &nbsp; &nbsp;<asp:LinkButton ID=LinkLast runat=server OnClick=LinkLast_Click>最後一頁</asp:LinkButton>

  &nbsp; &nbsp;轉到第<asp:DropDownList ID=DropDownList runat=server OnSelectedIndexChanged=DropDownList_SelectedIndexChanged >

  </asp:DropDownList>頁

  </form>

  </body>

  </html>

  注: PagedDataSource 類的部分公共屬性

  AllowCustomPaging  獲取或設置指示是否啟用自定義分頁的值

  AllowPaging   獲取或設置指示是否啟用分頁的值

  Count    獲取要從數據源使用的項數

  CurrentPageIndex   獲取或設置當前頁的索引

  DataSource   獲取或設置數據源

  DataSourceCount   獲取數據源中的項數

  FirstIndexInPage   獲取頁中的第一個索引

  IsCustomPagingEnabled  獲取一個值該值指示是否啟用自定義分頁

  IsFirstPage   獲取一個值該值指示當前頁是否是首頁

  IsLastPage   獲取一個值該值指示當前頁是否是最後一頁

  IsPagingEnabled   獲取一個值該值指示是否啟用分頁

  IsReadOnly   獲取一個值該值指示數據源是否是只讀的

  IsSynchronized   獲取一個值該值指示是否同步對數據源的訪問(線程安全)

  PageCount   獲取顯示數據源中的所有項所需要的總頁數

  PageSize   獲取或設置要在單頁上顯示的項數

  VirtualCount   獲取或設置在使用自定義分頁時數據源中的實際項數


From:http://tw.wingwit.com/Article/program/net/201311/12972.html
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.