2
<asp:CommandField ShowDeleteButton="True" />

如果用户确定要删除gridview中的记录,我如何将一个简单的jquery方法链接到只要求用户确认并返回(真或假)的删除按钮?

4

1 回答 1

1

您捕获该网格视图的删除按钮,并添加确认。此外,请注意不要覆盖现有命令。

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 中,则需要将 GridViewpageLoad()用于更新(版本 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 更新上运行。

于 2013-01-03T22:00:21.607 回答