当前位置: 代码迷 >> ASP.NET >> Command对象剔除数据 不懂
  详细解决方案

Command对象剔除数据 不懂

热度:6085   发布时间:2013-02-25 00:00:00.0
Command对象删除数据 不懂
public SqlConnection Getconnection()
  {
  string str = ConfigurationManager.AppSettings["ee"].ToString();
  SqlConnection myCon = new SqlConnection(str);
  return myCon;
  }
  public void bind()
  {
  SqlConnection myCon = Getconnection();
  myCon.Open();
  string str = "select * from tb_Class";
  SqlCommand myCom = new SqlCommand(str, myCon);
  SqlDataAdapter myDa = new SqlDataAdapter(myCom);
  DataSet myDs = new DataSet();
  myDa.Fill(myDs);
  GridView1.DataSource = myDs;
  GridView1.DataKeyNames = new string[] { "ClassID" };
  GridView1.DataBind();
  myDa.Dispose();
  myDs.Dispose();
  myCon.Close();
  }
  protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
  {
  SqlConnection myCon = Getconnection();
  myCon.Open();
  int ClassID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
  string str = "delete from tb_Class where ClassID=" + ClassID;
  SqlCommand myCom = new SqlCommand(str, myCon);
  myCom.ExecuteNonQuery();
  myCom.Dispose();
  myCon.Close();
  GridView1.EditIndex = -1;
  this.bind();
  }
  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  {
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
  ((LinkButton)e.Row.Cells[0].Controls[0]).Attributes.Add("onclick", "return confirm('确定要删除吗?')");
  }
  这段代码是,弹出一个"确定要删除吗?"的小窗口!!
具体的像(e.Row.RowType == DataControlRowType.DataRow)
((LinkButton)e.Row.Cells[0].Controls[0]).Attributes.Add
是什么意思
  }

------解决方案--------------------------------------------------------
代码的意思和你看到的效果一样,就是给一个a标签添加一个客户端单击事件
------解决方案--------------------------------------------------------
动态添加onclick事件
也就是点击时执行脚本return confirm('确定要删除吗?'
------解决方案--------------------------------------------------------
在GridView的行绑定事件中,判断如果行是数据行的话,通过FindControl找到LinkButton,然后为其添加客户端click事件,事件执行的代码就是弹出一个提示框,仅此而已。
------解决方案--------------------------------------------------------
e.Row.RowType == DataControlRowType.DataRow

这个判断语句的意思就是判断当前行是不是数据绑定行

((LinkButton)e.Row.Cells[0].Controls[0]).Attributes.Add

就像前幾樓說的給linkbutton按鈕添加一個js事件。彈出确定要删除吗這個窗口。
  相关解决方案