PagedDataSource pages;
protected void Page_Load(object sender, EventArgs e)
{
DatalistBind();
}
private void DatalistBind()
{
using (SqlConnection sqlcon = new SqlConnection("server=.;uid=sa;pwd=sa;database=xiaoxiDemo"))
{
int count = Convert.ToInt32(this.Label1.Text);
SqlDataAdapter sqlda = new SqlDataAdapter("select * from xi",sqlcon);
DataSet dt = new DataSet();
sqlda.Fill(dt, "xi");
pages = new PagedDataSource();
pages.DataSource = dt.Tables[0].DefaultView;
pages.AllowPaging = true;
pages.PageSize =6;
pages.CurrentPageIndex = count- 1;
this.DataList1.DataSource = pages;
this.DataList1.DataBind();
this.Button2.Enabled = true;
this.Button3.Enabled = true;
if (count == 1)
{
this.Button2.Enabled=false;
}
if (count==pages.Count)
{
this.Button3.Enabled = false;
}
}
}
//上一页
protected void Button2_Click(object sender, EventArgs e)
{
this.Label1.Text = Convert.ToString(Convert.ToInt32(this.Label1.Text)- 1);
DatalistBind();
}
//下一页
protected void Button3_Click(object sender, EventArgs e)
{
this.Label1.Text = Convert.ToString(Convert.ToInt32(this.Label1.Text)+1);
DatalistBind();
}
//删除
protected void LinkButton1_Command(object sender, CommandEventArgs e)
{
using (SqlConnection sqlcon = new SqlConnection("server=.;uid=sa;pwd=sa;database=xiaoxiDemo"))
{
sqlcon.Open();
SqlCommand sqlcmds = new SqlCommand("delete from xi where xi_id="+e.CommandArgument+"",sqlcon);
sqlcmds.ExecuteNonQuery();
sqlcon.Close();
}
}
//首页
protected void Button1_Click(object sender, EventArgs e)
{
this.Label1.Text = "1";
DatalistBind();
}
//尾页
protected void Button4_Click(object sender, EventArgs e)
{
this.Label1.Text = pages.Count.ToString();
DatalistBind();
}