【发布时间】:2012-12-18 08:16:05
【问题描述】:
<asp:CommandField ShowDeleteButton="True" />
如果用户确定是否要删除 gridview 中的记录,我如何将一个简单的 jquery 方法链接到仅要求用户确认并返回(真或假)的删除按钮?
【问题讨论】:
-
使用确认框..
<asp:CommandField ShowDeleteButton="True" />
如果用户确定是否要删除 gridview 中的记录,我如何将一个简单的 jquery 方法链接到仅要求用户确认并返回(真或假)的删除按钮?
【问题讨论】:
您捕获该网格视图的删除按钮,并添加确认。此外,请注意不要覆盖现有命令。
jQuery(document).ready(function()
{
// note: the `Delete` is case sensitive and is the title of the button
var DeleteButtons = jQuery('#<%= GridViewName.ClientID %> :button[value=Delete]');
DeleteButtons.each(function () {
var onclick = jQuery(this).attr('onclick');
jQuery(this).attr('onclick', null).click(function () {
if (confirm("Delete this record? This action cannot be undone...")) {
return onclick();
}
else
{
return false;
}
});
});
});
如果您将 GridView 放在 UpdatePanel 中,那么您需要使用 pageLoad() 进行更新(版本 4+):
function pageLoad()
{
var DeleteButtons = jQuery('#<%= GridViewName.ClientID %> :button[value=Delete]');
DeleteButtons.each(function () {
var onclick = jQuery(this).attr('onclick');
jQuery(this).attr('onclick', null).click(function () {
if (confirm("Delete this record? This action cannot be undone...")) {
return onclick();
}
else
{
return false;
}
});
});
}
pageLoad() 会在页面加载时运行,但也会在来自 UpdatePanel 的每次 ajax 更新时运行。
【讨论】: