jQuery Confirm Dialog in ASP.NET Button OnClientClick jQuery Confirm Dialog in ASP.NET Button OnClientClick asp.net asp.net

jQuery Confirm Dialog in ASP.NET Button OnClientClick


Check the selected answer for this question for an example: How to implement "confirmation" dialog in Jquery UI dialog?

A couple of notes:

Don't put your onclick functionality in an onclick attribute. One of the great benefits of jQuery is that it allows you to do Unobtrusive 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. 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" />