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();
}

C# GridView Edit & Delete, 点击Delete的时候弹出确认框的更多相关文章

  1. 请写出一段JavaScript代码,要求页面有一个按钮,点击按钮弹出确认框。程序可以判断出用

    请写出一段JavaScript代码,要求页面有一个按钮,点击按钮弹出确认框.程序可以判断出用 户点击的是“确认”还是“取消”. 解答: <HTML> <HEAD> <TI ...

  2. GridView控件中插入自定义删除按钮并弹出确认框

    GridView控件中插入自定义删除按钮,要实现这个功能其实有多种方法,这里先记下我使用的方法,以后再添加其他方法. 一.实现步骤 1.在GridView中添加模板列(TemplateField). ...

  3. [JS] 点击按钮触发后台事件前,弹出确认框

    只需要在button中设置onclick属性触发事件即可 下面以ASP.NET代码为例, ASP.NET中按钮客户端触发js代码的属性是OnClientClick <asp:Button ID= ...

  4. 从头开始一步一步实现EF6+Autofac+MVC5+Bootstarp极简前后台ajax表格展示及分页(二)前端修改、添加表格行点击弹出模态框

    在前一篇中,由于不懂jquery,前端做的太差了,今天做稍做修改,增加一个跳转到指定页面功能,表格行点击样式变化.并且在表格中加入bootstarp的按钮组,按钮点击后弹出模态框,须修改common, ...

  5. 实现对gridview删除行时弹出确认对话框的一种简单方法

    在VS2008提供的GridView中我们可以直接添加一个CommandField删除列:<asp:CommandField ShowDeleteButton="True" ...

  6. 【2017-05-21】WebForm跨页面传值取值、C#服务端跳转页面、 Button的OnClientClick属性、Js中getAttribute和超链接点击弹出警示框。

    一.跨页面传值和取值: 1.QueryString - url传值,地址传值 优缺点:不占用服务器内存:保密性差,传递长度有限. 通过跳转页面路径进行传值,方式: href="地址?key= ...

  7. WebForm跨页面传值取值、C#服务端跳转页面、 Button的OnClientClick属性和超链接点击弹出警示框

    一.跨页面传值和取值: 1.QueryString - url传值,地址传值 优缺点:不占用服务器内存:保密性差,传递长度有限. 通过跳转页面路径进行传值方式: href="地址?key=v ...

  8. 【代码笔记】iOS-点击搜索按钮,或放大镜后都会弹出搜索框

    一, 效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> #import "CLHSearchBar.h ...

  9. bootstrap 弹出框点击其他区域时弹出框不消失选项设置

    默认情况下,bootstrap 弹出框点击其他区域时,弹出框会自动关闭,在很多时候,我们可能会希望达到和原生弹出框一样的效果,避免不小心点击其他区域时弹框自动隐藏,尤其是对于一些复杂的表单,重复填写可 ...

随机推荐

  1. org.apache.lucene.queryParser.ParseException: Encountered "<EOF>" at line 1, column 0.

    如果出现了下列错误,那是因为用错了函数.把queryParser.Query改称queryParser.parse就通过了 org.apache.lucene.queryParser.ParseExc ...

  2. Basically Speaking

    Basically Speaking Time Limit: 2 Sec  Memory Limit: 200 MB Submit: 19  Solved: 11 [Submit][Status][W ...

  3. HDU 4738 Caocao's Bridges(割边)

    乍一看一个模板题,仔细一看还是模板题,但是三个坑.1,不是连通图,放0个.2 守卫为0,放1个. 3注意重边. #include<iostream> #include<cstdio& ...

  4. java 集合大家族

    在编写java程序中,我们最常用的除了八种基本数据类型,String对象外还有一个集合类,在我们的的程序中到处充斥着集合类的身影!java中集合大家族的成员实在是太丰富了,有常用的ArrayList. ...

  5. Linux + Apache + PHP 环境搭建

    搭建环境: Ubuntu 15.04 Apache 2.4.16 PHP 5.6.15 1 安装Apache 先安装依赖程序(都安装在 /usr/local/ 目录下) apr-1.5.2.tar.g ...

  6. 逆序一个8bit的2进制数

  7. jQuery两种扩展插件的方式

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  8. XAMPP命令之LAMPP

    .wiz-todo, .wiz-todo-img {width: 16px; height: 16px; cursor: default; padding: 0 10px 0 2px; vertica ...

  9. emacs command

    eval-buffer用来执行.emacs不要再重启了,或cxce执行光标前的一行 eval-region load-file ~/.emacs goto-line global-set-key定义快 ...

  10. 从零深入 tomcat

    1.tomcat的安装使用 tomcat是实现了J2EE标准的最简单轻巧的WEB服务器,所以使用tomcat必须安装JDK咯! ① 安装JDK并设置环境变量: ②设置tomcat的环境变量: 这两小步 ...