【问题标题】:Delete Gridview Row with Jquery使用 Jquery 删除 Gridview 行
【发布时间】:2012-12-18 08:16:05
【问题描述】:
<asp:CommandField ShowDeleteButton="True" />

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

【问题讨论】:

  • 使用确认框..

标签: jquery asp.net


【解决方案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 中,那么您需要使用 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 更新时运行。

【讨论】:

  • 您好,感谢您的回复,我是否应该将所有代码放在脚本标签 type = text/javascript 的头部,因为它不起作用?
  • @rikket 此代码有效,因为我正在使用它。在页面加载时放置,或者dom准备好(可以使用jQuery准备好),并且在每次UpdatePanel更新之后,如果使用UpdatePanels
  • 能否请您发送整个代码,因为我仍然无法使此代码工作(不知道放在哪里)谢谢!
  • 最后一个问题,如何更改对话框标题?
  • @rikket 是不可能的,只有当您使用自定义确认时。阅读有关以下内容的问题:stackoverflow.com/questions/43955/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-21
  • 2013-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-06
相关资源
最近更新 更多