jQuery replacement for javascript confirm

Posted by dcp on Stack Overflow See other posts from Stack Overflow or by dcp
Published on 2011-01-07T13:49:56Z Indexed on 2011/01/07 13:54 UTC
Read the original article Hit count: 236

Filed under:
|
|

Let's say I want to prompt the user before allowing them to save a record. So let's assume I have the following button defined in the markup:

<asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click"></asp:Button>

To force a prompt with normal javascript, I could wire the OnClick event for my save button to be something like this (I could do this in Page_Load):

btnSave.Attributes.Add("onclick",
    "return confirm('are you sure you want to save?');");

The confirm call will block until the user actually presses on of the Yes/No buttons, which is the behavior I want.

For the jquery dialog that is the equivalent, I tried something like this (see below). But the problem is that unlike javascript confirm(), it's going to get all the way through this function (displayYesNoAlert) and then proceed into my btnSave_OnClick method on the C# side. I need a way to make it "block", until the user presses the Yes or No button, and then return true or false so the btnSave_OnClick will be called or not called depending on the user's answer.

Currently, I just gave up and went with javascript's confirm, I just wondered if there was a way to do it.

function displayYesNoAlert(msg, closeFunction) {
    dialogResult = false;

    // create the dialog if it hasn't been instantiated
    if (!$("#dialog-modal").dialog('isOpen') !== true) {

        // add a div to the DOM that will store our message
        $("<div id=\"dialog-modal\" style='text-align: left;' title='Alert!'>").appendTo("body");

        $("#dialog-modal").html(msg).dialog({
            resizable: true,
            modal: true,
            position: [300, 200],
            buttons: {
                'Yes': function () {
                    dialogResult = true;
                    $(this).dialog("close");
                },
                'No': function () {
                    dialogResult = false;
                    $(this).dialog("close");
                }
            },
            close: function () {
                if (closeFunction !== undefined) {
                    closeFunction();
                }
            }
        });
    }
    $("#dialog-modal").html(msg).dialog('open');

}

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about jQuery