欢迎您访问我爱IT技术网,今天小编为你分享的编程技术是:【GridView添加删除按钮终极办法】,下面是详细的分享!
GridView添加删除按钮终极办法
这两天用GridView,正好遇到要添加删除确认这个操作,网上搜了一通,这篇最好,为gridview“删除”列添加确认对话框
其中提到一种方法被称为是一种最简洁的办法,果然不错,分析得也很好。
不过,我在用的过程中却遇到了哭笑不得的情况:引号的问题
<asp:CommandField EditText="修改"
CancelText="取消"
DeleteText='<span class="handle" onclick="JavaScript:return confirm("confirm_this")">删除</span>'
HeaderText="操作"
ButtonType="Link"
ShowEditButton="true"
ShowDeleteButton="true" />
看其中红色的部分,这部分代码是不对的,因为双引号会导致javascript错误,你想想,
<span class="handle" onclick="JavaScript:return confirm("confirm_this")">删除</span>
这四个引号能用么,不能。这里必须是一单一双的引号相间,代码才正常。
但也不能换成单引号,因为
<asp:CommandField EditText="修改" CancelText="取消" DeleteText='<span class="handle" onclick="JavaScript:return confirm('confirm_this')">删除</span>'...
因为,单引号会截断ASP.NET的代码,所以,单双引号都不能用。
不过,也有办法解决,如果doraeimo在blog中提到的方案称为最简洁,我想,下面的办法可以被称为最终极的解法,当然,这是建立在doraeimo的基础这上的。
解决方案:什么引号也有用!
<asp:CommandField
EditText="修改"
CancelText="取消"
DeleteText='<span class="handle" onclick="JavaScript:return confirm(confirm_this)">删除</span>'
HeaderText="操作"
ButtonType="Link"
ShowEditButton="true"
ShowDeleteButton="true" />
不过,要在后台中向客户端注册javascript脚本,在Page_Load事件中:
string sScript="var confirm_this='你确定要删除吗?';";
ClientScriptManager csm=Page.ClientScript;
if (!csm.IsStartupScriptRegistered("_confirm"))
{
csm.RegisterStartupScript(this.GetType(), "_confirm", sScript, true);
}
原理很简单,效果很实用。
以上所分享的是关于GridView添加删除按钮终极办法,下面是编辑为你推荐的有价值的用户互动:
相关问题:gridview如何为编辑和删除按钮添加确认提示
答:在后台的GridView1_RowDataBound()方法添加代码,最后代码如下所示:protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e){//如果是绑定数据行if (e.Row.RowType == DataControlRowType.DataRow){if (e.Row.RowState ... >>详细
相关问题:gridview自定义删除按钮,怎么实现。
答:这种情况可以用 GridView1_RowCommand事件 在删除按钮的linkbutton属性中添加 CommandArgument='' 然后在GridView1_RowCommand事件中代码如下 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { string id = e... >>详细
相关问题:C# Asp.Net中gridview中自带的删除按钮怎么用
答:楼主你好: GridView选中,编辑,取消,删除: 效果图: 后台代码:using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using Syste... >>详细
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
