views:

217

answers:

1

Hi everyone,

In an asp.net project I am currently working on, I need to create a modal popup that will act like the confirm() javascript function. However, the native confirm() function doesn't help out, since a custom design is required.

An earlier attempt for this using the SimpleModal jQuery plugin I split the confirmation and the actual action to 2 different LinkButtons, one of which was hidden. Clicking the "OK" button on the dialog would trigger the click event on the hidden LinkButton.

But this feels somewhat like an ugly hack.

Example

This was my early attempt:

var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_pageLoaded(function() {
    $('.confirmation').click(function(e) {
        e.preventDefault();

        // Find the action button.
        var button = $(this).next('.confirmation-action');

        confirmation($('.confirmation-popup'), function() {
            button.click();
        });
    });
});

function confirmation(element, callback) {
    $.modal(element, {
        // ...
        onShow: function(dialog) {
            // Bind both Yes and No buttons.
            dialog.data.find('.no').click(function() {
                $.modal.close();
            });
            dialog.data.find('.yes').click(function() {
                $.modal.close();
                if ($.isFunction(callback)) {
                    // Applies the callback, which in turn clicks the action LinkButton.
                    callback.apply();
                }
            });
        }
    });
}

Can anyone some provide different insight on this? Are there any other methods of doing this?

Is it even possible for a modal popup to return a value, like confirm() does?

Thanks!

A: 

When you click yes 'return true' and when you click no 'return false' almost literally

matpol
From my understanding of the code, the confirmation function would have to return a value.But $.modal() doesn't return a value I can work with, so this whole construct simply won't work.
GeReV
call a function from the click event get the return from this function and close the dialogue at the same time
matpol
Could you give an example? I don't think it would work just by calling 'return confirmation()', in my tests, $.modal() simply runs and returns regardless if or any buttons were clicked.
GeReV
function yes(){$.modal.close();return true}
matpol