Repeater讀取資料並分頁

來源:互聯網
上載者:User
分頁|資料

據說這個沒有什麼用,但是把這個搞懂了對datagri和datalist和有協助,事業就筆記一下了。
 ----------控制項清單---------
  Panel Panel1;
  Button Button1;
  Button Button2;
  Label Label1;
  Label Label2;
  Label Label3;
  Label Label4;
  Label Label5;
  Repeater Repeater1;

--------------------------.cs頁-----------------------------------
private void Page_Load(object sender, System.EventArgs e)
  {
   if(!this.IsPostBack)
   {
    this.Label1.Text="1";
    this.fill();
   }
  }
  private void fill()
  {
  int pag=Convert.ToInt32(this.Label1.Text);//設定當前頁
    SqlConnection con=new SqlConnection("server=.;database=Northwind;uid=sa;pwd=980123;");//執行個體化串連
    SqlDataAdapter sda=new SqlDataAdapter();//建立一個資料配接器對象
    sda.SelectCommand=new SqlCommand("select * from Employees",con);//執行個體化SelectCommand,並用他從資料庫讀出全部資料
    DataSet ds=new DataSet();//定義一個資料集填充
    sda.Fill(ds,"name");//使用適配器填充資料集到本地表“name”
   PagedDataSource ps=new PagedDataSource();//執行個體化一個PagedDataSource,這個本來是封裝是DATAGRID裡面的
   ps.DataSource=ds.Tables["name"].DefaultView;//設定他的資料來源為ds.Tables["name"].DefaultView資料檢視
   ps.AllowPaging=true;//允許分頁
   ps.PageSize=3;//每頁顯示數量
   ps.CurrentPageIndex=pag-1;//當前頁碼,因為頁是從0開始的,所以要減1
   this.Button1.Enabled=true;//按鈕目前狀態
   this.Button2.Enabled=true;
   this.Label5.Text=ps.PageCount.ToString();
   if(pag==1)
   {
   this.Button1.Enabled=false;//如果當前頁是  1 ,上一頁按鈕不可用
   }
   if(pag==ps.PageCount)
   {
   this.Button2.Enabled=false;//如果當前頁是最後一頁 ,下一頁按鈕不可用
   }
       this.Repeater1.DataSource=ps;
    this.Repeater1.DataBind();
  }
表單代碼
private void Button2_Click(object sender, System.EventArgs e)
  {
   this.Label1.Text=((Convert.ToInt32(this.Label1.Text))+1).ToString();
   this.fill();
  }

  private void Button1_Click(object sender, System.EventArgs e)
  {
  this.Label1.Text=((Convert.ToInt32(this.Label1.Text))-1).ToString();
    this.fill();
  }



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.