private void bind()
{
this.DataList1.DataKeyField="EmployeeID";//在绑定的时候指定绑定的主键,方便后面更新,删除使用
this.DataList1.DataSource=db.fill();
this.DataList1.DataBind();
}
private void DataList1_ItemCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{
if(e.CommandName=="show")
{
this.DataList1.SelectedIndex=e.Item.ItemIndex;//选中的行索引
this.bind();
}
}
private void DataList1_CancelCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{
this.DataList1.EditItemIndex=-1;
this.bind();
}
private void DataList1_EditCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{
this.DataList1.EditItemIndex=e.Item.ItemIndex;
this.bind();
}
private void DataList1_UpdateCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{//这里还有点问题,我没有更新到,懒得查,以后知道了再写
string ID=this.DataList1.DataKeys[e.Item.ItemIndex].ToString();//查出选中行在数据库中的主键
string city=((TextBox)e.Item.FindControl("txtcity")).Text;//查出修改文本框里的内容
SqlConnection con=db.creatcon();
SqlCommand cmd=new SqlCommand("update employees set city='"+city+"' where employeeID='"+ID+"'",con);
con.Open();
cmd.ExecuteNonQuery();
this.DataList1.EditItemIndex=-1;//选中索引为-1,表示一个都没有选中
this.bind();//重新绑定
}