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. VJ16216/RMQ/线段树单点更新

    题目链接 /* 单点更新,用RMQ维护最大值,add对c[i]修改,或加,或减. 求[l,r]的和,用sum(r)-sum(l-1).即可. */ #include<cmath> #inc ...

  2. Block 实现 浅析

    前言 这里 有关于 block 的 5 道测试题,建议你阅读本文之前先做一下测试. 先介绍一下什么是闭包.在 wikipedia 上,闭包的定义) 是: In programming language ...

  3. lucene的多种搜索2-SpanQuery

    SpanQuery按照词在文章中的距离或者查询几个相邻词的查询 SpanQuery包括以下几种: SpanTermQuery:词距查询的基础,结果和TermQuery相似,只不过是增加了查询结果中单词 ...

  4. Node.js学习 - CallBack Function

    Node.js异步编程的直接体现就是回调,Node使用了大量的回调函数,其所有的API都支持回调. 阻塞代码实例(同步) var fs = require("fs"); var d ...

  5. IOS小工具以及精彩的博客

    IOS小工具以及精彩的博客 工具 Log Guru是一个收集Log的小工具, 可以在 Mac 上查看 iOS 设备的实时系统日志. 现在可以直接高亮显示在 FIR.im 上安装 app 失败的原因.后 ...

  6. android 5.0新特性学习--Drawable Tinting(为图片资源着色)

    使用android:tint属性去调整色调.android:tintMode 着色模式 screen multiply and src_atop/src_in/src_oversetTint(int ...

  7. asp 特殊字符替换

    <%Function specialstr(yourstring)find= "¿,À,Á,Â,Ã,Ä,Å,Æ,Ç,È,É,Ê,Ë,Ì,Í,Î,Ï,Ð,Ñ,Ò,Ó,Ô,Õ,Ö,Ø,Ù, ...

  8. mac编译openssl扩展报错 openssl.c:44:10: fatal error: 'openssl/evp.h' file not found

    解决办法 brew link openssl --force 然后 ./configure --with-openssl --with-php-config=/usr/local/php/bin/ph ...

  9. PPTP-VPN日志功能,记录用户登录时间,流量统计,IP地址等信息

    我们先看两个文件 /etc/ppp/ip-up /etc/ppp/ip-down 这两个文件为shell脚本,当PPTP用户连接或者断开时分别执行这两个文件,并且会带相应的参数 这些参数有 $PEER ...

  10. DIL中基本数据类型

    (1)基本数据类型:OMG IDL基本数据类型包括short.long和相应的无符号(unsigned)类型,表示的字长分别为16.32位.  (2)浮点数类型:OMG IDL浮点数类型包括float ...