C# GridView Edit & Delete, 点击Delete的时候弹出确认框

时间:2023-03-08 16:21:45

1. 使用GridView自带属性ShowEditButton和ShowDeleteButton,均设为True

   <Columns>

    ...

    <asp:CommandField ShowEditButton="True" ShowDeleteButton="True"></asp:CommandField>

  </Columns>

则在GridView指定列的位置会显示Edit和Delete的LinkButton

2. 在GridView中添加Edit和Delete的响应函数

点击Edit按钮后,该行纪录进入编辑模式,同时Edit按钮变为Update和Cancle两个按钮,因此需添加与Edit相关的3个函数

点击Delete按钮后弹出确认框,此事件需要在RowDataBound方法中绑定,因此需要为GridView添加RowDataBound函数

  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id"

      ...

      OnRowDataBound="GridView1_RowDataBound"
      OnRowEditing="GridView1_RowEditing"
      OnRowCancelingEdit="GridView1_RowCancelingEdit"
      OnRowUpdating="GridView1_RowUpdating"
      OnRowDeleting="GridView1_RowDeleting"
      ...

  >

3. 在RowDataBound中为Delete按钮绑定弹出确认框

点击Delete前按钮为 Edit Delete

点击Delete后按钮为 Update Cancle

为Delete按钮绑定弹出确认框事件时,首先需要获取Delete按钮,事实上无法直接获取Delete按钮,只能通过Delete按钮的位置来获取(如是当前行第几列的第几个按钮),进而为此按钮绑定事件

为Delete按钮绑定弹出确认框事件后,发现GridView隔行出现:点击Edit -> Cancle弹出“确定删除?”确认框

由于GridView中RowType有Header、Footer、DataRow、Pager等类型,一般只有数据行(类型为DataRow)才会有Delete按钮,因此首先应判断是否为数据行

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

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

    {

      //数据行每一行第6列第2个控件为Delete按钮,第一个控件为Edit按钮(下标为0),Edit按钮和Delete按钮之间还有一个空格(下标为1)
      LinkButton lbtn = (LinkButton)e.Row.Cells[6].Controls[2];

      //判断所取得控件的名称是否为Delete,为了避免点击Edit后出现Update Cancle会为Cancle按钮绑定弹出窗口的情况
      if (lbtn.CommandName == "Delete")
      {
        lbtn.OnClientClick = "return confirm('Do you really want to delete?');";
      }
    }

  }

4. GridView Edit

  //点击某行Edit按钮该行进入编辑状态

  protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)

  {
    GridView1.EditIndex = e.NewEditIndex;

    //DataBind是GridView数据源绑定函数
    DataBind();
  }

  //将某行编辑后的值更新到数据库

  protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
  {
    int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
    String name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.ToString().Trim();
    ...

    int flag = updateDatabase();
    if (flag == 0)
    {
      Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('Updated Successfully!');</script>");
    }
    else
    {
      Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('Failed to update!');</script>");
    }

    GridView1.EditIndex = -1;
    DataBind();
  }

  //取消编辑

  protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

  {
    GridView1.EditIndex = -1;
    DataBind();
  }

5. GridView Delete

  protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
  {
    String id = GridView1.DataKeys[e.RowIndex].Value.ToString();
    int flag = deleteFromDatabase();
    if (flag == 0)
    {
      Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('Deleted Successfully!');</script>");
    }
    else
    {
      Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('Failed to delete!');</script>");
    }

    DataBind();
}