【问题标题】:jQuery Confirm Dialog in ASP.NET Button OnClientClickASP.NET 按钮 OnClientClick 中的 jQuery 确认对话框
【发布时间】:2011-05-02 02:13:14
【问题描述】:

我在 UpdatePanel 的 GridView 中有一个 TemplateField,带有一个名为 btnDelete 的按钮。而不是标准的OnClientClick="return confirm('Are you sure?')",我想使用jQuery Dialog。

到目前为止,我可以使用 btnDelete.Attributes["onclick"] 设置 jQuery 并在代码隐藏中设置 jQuery 对话框代码。但是,在我有机会单击“确认”或“取消”之前,它在所有情况下都会发回服务器。

这是它生成的 HTML:

<input type="submit" rel="Are you sure?" class="action-link delete" id="ctl00_c1_gvTransfers_ctl02_btnDelete" onclick="return function() { 
            $('#delete-transfer-confirm').dialog({
              buttons: {
                'Confirm' : function() { $(this).dialog('close'); return true; },
                'Cancel'  : function() { $(this).dialog('close'); return false; }
              }
            });

            $('p.message').text($(this).attr('rel'));
            $('#delete-transfer-confirm').dialog('open');
          };" value="Delete" name="ctl00$c1$gvTransfers$ctl02$btnDelete">

我做错了什么导致此功能在单击任一按钮之前不会阻塞?

相反,标准确认工作正常:

<input type="submit" class="action-link delete" id="ctl00_c1_gvTransfers_ctl02_btnDelete" onclick="try{if (!window.confirm('Are you sure?')){return false;};}catch(e1){alert(&quot;Unexpected Error:\n\n&quot; + e1.toString());return false;};" value="Delete" name="ctl00$c1$gvTransfers$ctl02$btnDelete">

谢谢, 标记

更新:

最终,我不得不使用 UseSubmitBehavior="false" 来获取要呈现的 name="" 属性。然后我不得不重写 OnClientClick,将值设置为“return;”所以默认的 __doPostBack() 不会被执行。然后我能够连接一个 .live() 点击处理程序,它在 Confirm 时调用 __doPostBack():

$('input.delete').live('click', function(e) {
        var btnDelete = $(this);
        alert($(btnDelete).attr('name'));
        e.preventDefault();


        $('#delete-transfer-confirm').dialog({
          buttons: {
            'Confirm': function() {
              $(this).dialog('close');
              __doPostBack($(btnDelete).attr('name'), '');

              return true;
            },
            'Cancel': function() {
              $(this).dialog('close');
              return false;
            }
          }
        });

        $('p.message').text($(this).attr('rel'));
        $('#delete-transfer-confirm').dialog('open');
      });

【问题讨论】:

标签: asp.net jquery jquery-ui dialog confirm


【解决方案1】:

查看所选答案的示例:How to implement "confirmation" dialog in Jquery UI dialog?

几点说明:

不要将您的 onclick 功能放在 onclick 属性中。 jQuery 的一大好处是它允许您执行 Unobtrusive Javascript。相反,请执行以下操作:

$(function() {
    $('.delete').click(function(e) {
        e.preventDefault() //this will stop the automatic form submission
        //your functionality here
    });
});

另外,请确保您的对话框在单击事件之外进行实例化,以便在第一次单击事件发生之前对其进行初始化。因此,您的结果将是这样的:

$(function() {
     $("#delete-transfer-confirm").dialog({
      autoOpen: false,
      modal: true
    });

    $('.delete').click(function(e) {
        e.preventDefault();
        $('#delete-transfer-confirm').dialog({
            buttons: {
                'Confirm': function() {
                    $(this).dialog('close');
                    return true;
                },
                'Cancel': function() {
                    $(this).dialog('close');
                    return false;
                }
            }
        });

        $('p.message').text($(this).attr('rel'));
        $('#delete-transfer-confirm').dialog('open');
    });
});

这应该对你有用。

【讨论】:

    【解决方案2】:
    $(document).ready(function () {
    
            $('#btnCancel').click(function (e) {
                e.preventDefault();
    
                $("<div><span><b>Are you sure you want to cancel this order?</b></span></div>").dialog({
                    modal: true,
                    draggable: false,
                    resizable: false,
                    width: 430,
                    height: 150,
                    buttons: {
                        "No": function () {
                            $(this).dialog("destroy");
    
                        },
                        "Yes": function () {
                            $("#btnCancel").unbind();
                            $(this).dialog("destroy");
                            document.getElementById('<%= btnCancel.ClientID %>').click();
    
                        }
                    }
                });
            });
    
        });
    

    然后在页面正文中

     <asp:button id="btnCancel" runat="server" cssclass="button_major" text="Cancel" style="float: right"
                        onclick="btnCancel_ClickEvent" clientidmode="Static" />
    

    【讨论】:

      猜你喜欢
      • 2012-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多