使用PagedDataSource類實現分頁

      Asp.net提供了三個功能強大的列表控件:GridView、DataList和Repeater控件,相對GridView,DataList和Repeater控件具有更高的樣式自定義性,很多時候我們喜歡使用DataList或Repeater控件來顯示數據,但是Repeater和DataList沒有分頁功能,有時很不方便。

      PagedDataSource類封裝了GridView控件的屬性,從而使GridView控件可以執行分頁,它就是一個數據的容器,我們先把數據從數據庫中讀取出來放在這個容器中,然後設置容器的屬性取出當前要顯示的頁上的部分數據,然後將此部分數據再綁定到頁面上的顯示控件上。

     下面實例是用PagedDataSource類實現DataList控件的數據分頁
頁面後臺代碼
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls;
using System.Data;
using System.Data.SqlClient;

public partial class Demo : System.Web.UI.Page
{
 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=127.0.0.1;database=ObtainEmployment;user id=sa;password=;");//取連接字符串,建立連接
  da = new SqlDataAdapter();
  da.SelectCommand = new SqlCommand("SELECT top 50 * FROM db_Article where checkup='1' ORDER BY intime DESC ", conn);
  ds = new DataSet();
  try
  {
   conn.Open();
   da.Fill(ds, "Article");
   conn.Close();
  }   catch (SqlException e1)
  {
   Response.Write(e1.ToString());
  }
 int cup = Convert.ToInt32(this.lb_CurrentPage.Text); //當前頁數,初始化爲地1頁
 PagedDataSource ps = new PagedDataSource();
 ps.DataSource = ds.Tables["Article"].DefaultView;
 ps.AllowPaging = true; ps.PageSize = 6; //每頁顯示的數據的行數
 ps.CurrentPageIndex = cup - 1;
 lb_count.Text = ps.DataSourceCount.ToString(); //獲取記錄總數
 lb_page.Text = ps.PageCount.ToString(); //獲取總頁數
 if (!IsPostBack)
 {
  for (int i = 1; i < ps.PageCount + 1; i++)
  {
   this.DropDownList1.Items.Add(i.ToString());
  }
   LinkUp.Enabled = true;
   LinkDown.Enabled = true;
 }
  try
 {
  DropDownList1.SelectedItem.Text =cup.ToString();
  DataList1.DataSource = ps;
  DataList1.DataBind();
 }
 catch (Exception ex) { throw ex; }
}
 protected void LinkDown_Click(object sender, EventArgs e) //下一頁按鈕代碼
{
 try {
   lb_CurrentPage.Text = Convert.ToString(Convert.ToInt32(lb_CurrentPage.Text) + 1);
   DropDownList1.SelectedValue = lb_CurrentPage.Text;
   getArticle();
 }
 catch (Exception ex)
 {   Response.Write("");
  lb_CurrentPage.Text = "1";
  getArticle();
 }
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) //跳轉到指定頁代碼
{
 int page =Convert.ToInt16((DropDownList1.SelectedItem.Value));
 lb_CurrentPage.Text = page.ToString();
 getArticle();
}
protected void LinkUp_Click(object sender, EventArgs e) //上一頁按鈕代碼
{
 try
 {
  if (Convert.ToInt16(lb_CurrentPage.Text) > 1)
  {
   lb_CurrentPage.Text = Convert.ToString(Convert.ToInt32(lb_CurrentPage.Text) - 1);
   DropDownList1.SelectedValue = lb_CurrentPage.Text; getArticle();
  }
  else
  {    Response.Write("");
  }
 }
 catch (Exception ex)
 { Response.Write(""); }
}
protected void LinkFirst_Click(object sender, EventArgs e) //跳到第一頁代碼
{
 if (lb_CurrentPage.Text != "1")
  lb_CurrentPage.Text= "1";
 else
 { Response.Write(""); }
 getArticle();
}
protected void LinkLast_Click(object sender, EventArgs e) //跳到最後一頁代碼
{
 if (lb_CurrentPage.Text.ToString() !=lb_page.Text.ToString())   lb_CurrentPage.Text = lb_page.Text.ToString();
 else
 {  Response.Write(""); }
getArticle();
}
}
頁面前臺代碼 注: PagedDataSource 類的部分公共屬性:
AllowCustomPaging 獲取或設置指示是否啓用自定義分頁的值。
AllowPaging 獲取或設置指示是否啓用分頁的值。
Count 獲取要從數據源使用的項數。 CurrentPageIndex 獲取或設置當前頁的索引。
DataSource 獲取或設置數據源。
DataSourceCount 獲取數據源中的項數。
FirstIndexInPage 獲取頁中的第一個索引。
IsCustomPagingEnabled 獲取一個值,該值指示是否啓用自定義分頁。
IsFirstPage 獲取一個值,該值指示當前頁是否是首頁。
IsLastPage 獲取一個值,該值指示當前頁是否是最後一頁。
IsPagingEnabled 獲取一個值,該值指示是否啓用分頁。
IsReadOnly 獲取一個值,該值指示數據源是否是隻讀的。
IsSynchronized 獲取一個值,該值指示是否同步對數據源的訪問(線程安全)。
PageCount 獲取顯示數據源中的所有項所需要的總頁數。 PageSize 獲取或設置要在單頁上顯示的項數。 VirtualCount 獲取或設置在使用自定義分頁時數據源中的實際項數。

發佈了16 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章