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. 下拉刷新和上拉加载 Swift

    转载自:http://iyiming.me/blog/2015/07/05/custom-refresh-and-loading/ 关于下拉刷新和上拉加载,项目中一直使用MJRefresh(原先还用过 ...

  2. - (void)addAnimation:(CAAnimation *)anim forKey:(nullable NSString *)key; 方法浅析

    转载自:http://blog.csdn.net/ronaldo_carry/article/details/49070119 将viewdidload里面的代码全部注释掉 - (void)viewD ...

  3. 介绍两个Android不常用的Drawable:GradientDrawable和 StateListDrawable

    //-------------------------------------------------------------------------------------------------- ...

  4. css05 字体以及行间距

    <head><meta charset="utf-8"><title>无标题文档</title><style>#box{ ...

  5. CenOS配置VSFTP服务器

    1 Linux FTP服务器分类: wu-ftp proftp=profession ftp vsftp=very security ftp 2 安装vsftp yum install vsftp 3 ...

  6. C#中:函数访问级别对函数形参访问级别的约束

    Inconsistent accessibility: parameter type 'Program.CommandLineInfo' is less accessible than method ...

  7. Servlet程序开发-Helloworld

    D:\Workspace\WEB-INF\classes下新建HelloServlet.java文件: package org.lxh.servletdemo ; import java.io.* ; ...

  8. Learning

    这里收录了2015年11月1日以来的githubOC排行版的框架,在此之前,已有前人收录的许多iOS开发框架,可以先参考: 4个优秀的总结:       ?Github-iOS备忘 (国人总结的上百个 ...

  9. Android三种左右滑动效果 手势识别

    Android三种左右滑动效果 手势识别(转)   手势识别 1.onCreate中添加GestureDetector mGestureDetector; //监听手势事件 mGestureDetec ...

  10. Mahout分布式运行实例:基于矩阵分解的协同过滤评分系统(一个命令实现文件格式的转换)

     Apr 08, 2014  Categories in tutorial tagged with Mahout hadoop 协同过滤  Joe Jiang 前言:之前配置Mahout时测试过一个简 ...