It is said that this is of little use, but understanding this will be helpful to datagri and datalist, so I will take notes on my career.
----------Control List---------
Panel Panel1;
Button Button1;
Button Button2;
Label Label1;
Label Label2;
Label Label3;
Label Label4;
Label Label5;
Repeater Repeater1;
----------------------------------.cs page ---------- --------------------------------
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);//Set the current page
SqlConnection con=new SqlConnection("server=.;database=Northwind;uid=sa;pwd=980123;");//instantiate the connection
SqlDataAdapter sda=new SqlDataAdapter();//Create a data adapter object
sda.SelectCommand=new SqlCommand("select * from Employees",con);//Instantiate SelectCommand and use it to read all data from the database
DataSet ds=new DataSet();//Define a data set to fill
sda.Fill(ds,"name");//Use the adapter to fill the data set to the local table "name"
PagedDataSource ps=new PagedDataSource();//Instantiate a PagedDataSource, which is originally encapsulated in DATAGRID
ps.DataSource=ds.Tables["name"].DefaultView;//Set his data source to ds.Tables["name"].DefaultView data view
ps.AllowPaging=true;//Allow paging
ps.PageSize=3;//Number of displays per page
ps.CurrentPageIndex=pag-1;//The current page number, because the page starts from 0, so it needs to be reduced by 1
this.Button1.Enabled=true;//Current status of the button
this.Button2.Enabled=true;
this.Label5.Text=ps.PageCount.ToString();
if(pag==1)
{
this.Button1.Enabled=false;//If the current page is 1, the previous page button is not available
}
if(pag==ps.PageCount)
{
this.Button2.Enabled=false;//If the current page is the last page, the next page button is not available
}
this.Repeater1.DataSource=ps;
this.Repeater1.DataBind();
}
form code
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();
}