分頁|資料
據說這個沒有什麼用,但是把這個搞懂了對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();
}