在GridView控件中刪除數據

来源:互联网 发布:类似360的软件 编辑:程序博客网 时间:2024/06/08 14:27

添加一個CommandField列并指明為“刪除”按鈕(默認為LinkButton按鈕),單擊該按鈕時將觸發RowDeleting事件。

例如:

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

{

          string sqlStr = "delete from tb_Member where id = '" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";

          sqlConn = new SqlConnection(strConn);

          sqlCmd = new SqlCommand(sqlStr, sqlConn);

          sqlConn.Open();

          sqlCmd.ExecuteNoQuery();

          sqlConn.Close();

          bind();

}

 

單擊刪除按鈕時可以設置彈出一個確認提示框,例如:

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

{

          if(e.Row.RowType == DataControlRowType.DataRow)

          {

                    ((LinkButton)(e.Row.Cell[4].Controls[0])).Attributes.Add("onclick", "return confirm('確認要刪除嗎?')");

          }

}

原创粉丝点击