據(jù)說這個沒有什么用,但是把這個搞懂了對datagri和datalist和有幫助,事業(yè)就筆記一下了。
----------控件清單---------
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);//設(shè)置當(dāng)前頁
SqlConnection con=new SqlConnection("server=.;database=Northwind;uid=sa;pwd=980123;");//實例化連接
SqlDataAdapter sda=new SqlDataAdapter();//建立一個數(shù)據(jù)適配器對象
sda.SelectCommand=new SqlCommand("select * from Employees",con);//實例化SelectCommand,并用他從數(shù)據(jù)庫讀出全部數(shù)據(jù)
DataSet ds=new DataSet();//定義一個數(shù)據(jù)集填充
sda.Fill(ds,"name");//使用適配器填充數(shù)據(jù)集到本地表“name”
PagedDataSource ps=new PagedDataSource();//實例化一個PagedDataSource,這個本來是封裝是DATAGRID里面的
ps.DataSource=ds.Tables["name"].DefaultView;//設(shè)置他的數(shù)據(jù)源為ds.Tables["name"].DefaultView數(shù)據(jù)視圖
ps.AllowPaging=true;//允許分頁
ps.PageSize=3;//每頁顯示數(shù)量
ps.CurrentPageIndex=pag-1;//當(dāng)前頁碼,因為頁是從0開始的,所以要減1
this.Button1.Enabled=true;//按鈕當(dāng)前狀態(tài)
this.Button2.Enabled=true;
this.Label5.Text=ps.PageCount.ToString();
if(pag==1)
{
this.Button1.Enabled=false;//如果當(dāng)前頁是 1 ,上一頁按鈕不可用
}
if(pag==ps.PageCount)
{
this.Button2.Enabled=false;//如果當(dāng)前頁是最后一頁 ,下一頁按鈕不可用
}
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();
}
新聞熱點
疑難解答