描述:DataList通过模板自定义实现分页,增删改查等操作
注意两个引用
using System.Data;
using System.Data.SqlClient;
public void datalist()
{
PagedDataSource ps = new PagedDataSource(); //实例化数据源
int dangpage = Convert.ToInt32(danglbl.Text); //定义当前页面
SqlConnection con = new SqlConnection("server=.;database=student;integrated security=true");
SqlDataAdapter da = new SqlDataAdapter("select * from stuInfo", con);
DataSet ds = new DataSet();
da.Fill(ds);
ps.DataSource = ds.Tables[0].DefaultView; //显示数据
ps.AllowPaging = true; //允许分页
ps.PageSize = 2; //每页显示的数据条数
ps.CurrentPageIndex = dangpage - 1; //当前页的索引
lbtf.Enabled = true;
lbts.Enabled = true;
lbtx.Enabled = true;
lbth.Enabled = true;
if (dangpage == 1)
{
lbtf.Enabled = false;
lbts.Enabled = false;
}
else if (dangpage == ps.PageCount)
{
lbtx.Enabled = false;
lbth.Enabled = false;
}
zonglbl.Text = ps.PageCount.ToString(); //总页数
DataList1.DataSource = ps; //绑定分页数据源
DataList1.Caption = "学生信息表"; //定义表名
DataList1.DataKeyField="sno"; //设置主键
DataList1.DataBind();
}
//首页
protected void lbtf_Click(object sender, EventArgs e)
{
danglbl.Text= "1";
this.datalist();
}
//上一页
protected void lbts_Click(object sender, EventArgs e)
{
danglbl.Text = (Convert.ToInt32(danglbl.Text) - 1).ToString();
this.datalist();
}
//下一页
protected void lbtx_Click(object sender, EventArgs e)
{
danglbl.Text = (Convert.ToInt32(danglbl.Text) + 1).ToString();
this.datalist();
}
//尾页
protected void lbth_Click(object sender, EventArgs e)
{
danglbl.Text = zonglbl.Text;
this.datalist();
}
//查看详细信息
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "select")
{
DataList1.SelectedIndex = e.Item.ItemIndex;
this.datalist();
}
else if (e.CommandName == "back")
{
DataList1.SelectedIndex = -1;
this.datalist();
}
}
//编辑信息
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{
DataList1.EditItemIndex = e.Item.ItemIndex;
this.datalist();
}
//更新信息
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
string no = DataList1.DataKeys[e.Item.ItemIndex].ToString();
string name = ((TextBox)e.Item.FindControl("TextBox2")).Text;
string sex = ((TextBox)e.Item.FindControl("TextBox3")).Text;
string age = ((TextBox)e.Item.FindControl("TextBox4")).Text;
string adr= ((TextBox)e.Item.FindControl("TextBox5")).Text;
SqlConnection con = new SqlConnection("server=.;database=student;integrated security=true");
con.Open();
SqlCommand cmd = new SqlCommand("update stuInfo set sname='" + name + "',ssex='"+sex+"', sage='"+age+"', adress='"+adr+"' where sno='" + no + "'", con);
cmd.ExecuteNonQuery();
con.Close();
DataList1.EditItemIndex = -1;
this.datalist();
}
//取消编辑
protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
{
DataList1.EditItemIndex = -1;
this.datalist();
}
}
//删除信息
protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "delete")
{
string id = DataList1.DataKeys[e.Item.ItemIndex].ToString();
modeljiangjin.jjid = Convert.ToInt32(id);
dal.jiangjinDAL djd = new dal.jiangjinDAL();
bool ds = djd.Delectjiangjin(modeljiangjin);
if (ds == true)
{
//DataList1.EditItemIndex = -1;
this.BDdeletejiangjin();
}
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('删除成功!'); ", true);
//Response.Redirect("/admin/a_deletejiangjin");
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('对不起,删除失败,请重新操作!'); ", true);
}
}
}