C# GridView Edit & Delete, 点击Delete的时候弹出确认框
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的时候弹出确认框的更多相关文章
- 请写出一段JavaScript代码,要求页面有一个按钮,点击按钮弹出确认框。程序可以判断出用
请写出一段JavaScript代码,要求页面有一个按钮,点击按钮弹出确认框.程序可以判断出用 户点击的是“确认”还是“取消”. 解答: <HTML> <HEAD> <TI ...
- GridView控件中插入自定义删除按钮并弹出确认框
GridView控件中插入自定义删除按钮,要实现这个功能其实有多种方法,这里先记下我使用的方法,以后再添加其他方法. 一.实现步骤 1.在GridView中添加模板列(TemplateField). ...
- [JS] 点击按钮触发后台事件前,弹出确认框
只需要在button中设置onclick属性触发事件即可 下面以ASP.NET代码为例, ASP.NET中按钮客户端触发js代码的属性是OnClientClick <asp:Button ID= ...
- 从头开始一步一步实现EF6+Autofac+MVC5+Bootstarp极简前后台ajax表格展示及分页(二)前端修改、添加表格行点击弹出模态框
在前一篇中,由于不懂jquery,前端做的太差了,今天做稍做修改,增加一个跳转到指定页面功能,表格行点击样式变化.并且在表格中加入bootstarp的按钮组,按钮点击后弹出模态框,须修改common, ...
- 实现对gridview删除行时弹出确认对话框的一种简单方法
在VS2008提供的GridView中我们可以直接添加一个CommandField删除列:<asp:CommandField ShowDeleteButton="True" ...
- 【2017-05-21】WebForm跨页面传值取值、C#服务端跳转页面、 Button的OnClientClick属性、Js中getAttribute和超链接点击弹出警示框。
一.跨页面传值和取值: 1.QueryString - url传值,地址传值 优缺点:不占用服务器内存:保密性差,传递长度有限. 通过跳转页面路径进行传值,方式: href="地址?key= ...
- WebForm跨页面传值取值、C#服务端跳转页面、 Button的OnClientClick属性和超链接点击弹出警示框
一.跨页面传值和取值: 1.QueryString - url传值,地址传值 优缺点:不占用服务器内存:保密性差,传递长度有限. 通过跳转页面路径进行传值方式: href="地址?key=v ...
- 【代码笔记】iOS-点击搜索按钮,或放大镜后都会弹出搜索框
一, 效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> #import "CLHSearchBar.h ...
- bootstrap 弹出框点击其他区域时弹出框不消失选项设置
默认情况下,bootstrap 弹出框点击其他区域时,弹出框会自动关闭,在很多时候,我们可能会希望达到和原生弹出框一样的效果,避免不小心点击其他区域时弹框自动隐藏,尤其是对于一些复杂的表单,重复填写可 ...
随机推荐
- easyui formatter 返回easyui组件
<table id="dg2" title="标题" style="width:400px;float: left;"> < ...
- Swift中的闭包(Closure) 浅析
转载自:http://www.devtalking.com/articles/closure-expressions-in-swift/ 闭包在Swift中非常有用.通俗的解释就是一个Int类型里存储 ...
- RatingBar
题记:保持旺盛的求知欲.希望会一直这样. 说明:来了新控件了.就是经常用的打分的那种东东. 说明:1.看上图分别是系统自带的和自己做的.rating就是设置小星星的数目. 2.用系统自带的必须是Wra ...
- toad执行存储过程
选中存储过程,右键执行,根据存储过程中的sql语句,输入参数, 执行结果的out值点击connection output,鼠标放在statement标签,就会显示下边的out值. 在db2cmd下执行 ...
- 2016.10.08--Intel Code Challenge Final Round--D. Dense Subsequence
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- 修改TabPageIndicator下划线的颜色
<style name="CustomTabPageIndicator" parent="Widget.TabPageIndicator"> < ...
- debug经验汇总
(1)使用pstack (2)调试core文件 # gdb ./segment core (3)使用strace strace -tt -f -s 1234 -o /tmp/strace.cwc -p ...
- Javascript面向对象编程(二):构造函数的继承
这个系列的第一部分,主要介绍了如何"封装"数据和方法,以及如何从原型对象生成实例. 今天要介绍的是,对象之间的"继承"的五种方法. 比如,现在有一个" ...
- MySQL架构优化:定时计划任务与表分区
转自: MySQL架构优化实战系列3:定时计划任务与表分区 - 今日头条(TouTiao.com)http://toutiao.com/a6304736482361049345/?tt_from=mo ...
- Mysql命令-求一列字段的总和
求和命令 mysql> select SUM(price) from order where create_time>'2016-03-12';+------------+| SUM(pr ...