简体   繁体   English

ASP.NET按钮OnClientClick中的jQuery确认对话框

[英]jQuery Confirm Dialog in ASP.NET Button OnClientClick

I have a TemplateField in a GridView in an UpdatePanel with a button called btnDelete . 我在UpdatePanel的GridView中有一个TemplateField,带有一个名为btnDelete的按钮。 Rather than the standard OnClientClick="return confirm('Are you sure?')" I'd like to use jQuery Dialog. 我想使用jQuery Dialog,而不是标准的OnClientClick="return confirm('Are you sure?')"

So far, I'm able to set the jQuery using btnDelete.Attributes["onclick"] and setting the jQuery Dialog code in the code-behind. 到目前为止,我已经能够使用btnDelete.Attributes["onclick"]设置jQuery并在后面的代码中设置jQuery Dialog代码。 However, it posts back to the server in all cases before I have a chance to click "Confirm" or "Cancel". 但是,在所有情况下,它都会发回到服务器,然后才有机会单击“确认”或“取消”。

Here is the HTML it produces: 这是它产生的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">

What am I doing wrong that is causing this function not to block until either button is clicked? 我在做什么错导致该功能在单击任一按钮之前都无法阻止?

Conversely, the standard confirm works just fine: 相反,标准确认可以正常工作:

<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">

Thanks, Mark 谢谢马克

UPDATE: 更新:

Ultimately, I had to use UseSubmitBehavior="false" to get the name="" attribute to render. 最终,我不得不使用UseSubmitBehavior =“ false”来获取name =“”属性进行渲染。 Then I had to override the OnClientClick, setting the value to "return;" 然后,我必须重写OnClientClick,将值设置为“ return;”。 so the default __doPostBack() doesn't get executed. 因此不会执行默认的__doPostBack()。 Then I was able to wire up a .live() click handler, which invokes the __doPostBack() on Confirm: 然后,我可以连接一个.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');
      });

Check the selected answer for this question for an example: How to implement "confirmation" dialog in Jquery UI dialog? 以示例的方式检查所选问题的答案: 如何在Jquery UI对话框中实现“确认”对话框?

A couple of notes: 一些注意事项:

Don't put your onclick functionality in an onclick attribute. 不要将onclick功能放在onclick属性中。 One of the great benefits of jQuery is that it allows you to do Unobtrusive Javascript. jQuery的一大优点是它允许您执行不引人注目的Javascript。 Instead, do something like this: 相反,请执行以下操作:

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

Also, make sure that your dialog is instantiated outside the click event, so that it is initialized before the first click event happens. 另外,请确保您的对话框是在click事件之外实例化的,以便在第一次click事件发生之前对其进行初始化。 So, something like this would be your result: 因此,这样的结果将是您的结果:

$(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');
    });
});

That should do the trick for you. 那应该为您解决问题。

$(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();

                    }
                }
            });
        });

    });

Then in the Page Body 然后在页面正文中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM