views:

1480

answers:

4

Consider an ASP.NET webforms app where the requirement is to raise a confirm dialog when an asp:LinkButton is clicked. The results of the confirm dialog should allow or block the postback for the linkButton_click method.

The plugin/library currently being used is the jquery.alerts.js (found on ABeautifulSite).

<script src="http://labs.abeautifulsite.net/projects/js/jquery/alerts/demo/jquery.js"
    type="text/javascript"></script>
<script src="http://labs.abeautifulsite.net/projects/js/jquery/alerts/demo/jquery.ui.draggable.js"
    type="text/javascript"></script>
<!-- Core files -->
<script src="http://labs.abeautifulsite.net/projects/js/jquery/alerts/demo/jquery.alerts.js"
    type="text/javascript"></script>
<link href="http://labs.abeautifulsite.net/projects/js/jquery/alerts/demo/jquery.alerts.css"
    rel="stylesheet" type="text/css"   />

     <asp:LinkButton runat="server" ID="lnkDisable" 
Text="Disable This Emp" onclick="lnkDisable_Click" />

<script type="text/javascript">
    $(document).ready(function() {

        $("#ctl00_mainContent_lnkDisable").click(function() {
            jConfirm('Please confirm this emp should be disabled?', 
                     'Confirm Disable', 
                     function(r) {
                                jAlert('Confirmed: ' + r, 'Results');
                });
            }); 
        });         
</script>

alt text

The message box fires properly!

Problem The problem is that it shows for about 2 seconds, and then the page is posted-back before the user can make a selection on the popup. I've set a breakpoint on that event handler, and indeed the page is posted back.

Question How can I have the post-back delayed while the user makes up their mind and clicks on the OK or Cancel button on the popup?

Any suggestions on another plugin are welcome!

+3  A: 

I think you'll want:

return jConfirm('Please confirm this emp should be disabled?',

(Note addition of 'return' statement).

But I'm not 100% certain how the 'jConfirm' function works.

-- Edit

It's possible (probable, in fact) that that dialog isn't modal. See if you can figure out how to make it so (or more likely someone will reply).

With a typical JavaScript confirm though you can do ...

return confirm("Are you sure?");

But I'm assuming you want the pretty one :)

-- Edit

Given it can't be made modal, perhaps something like:

    var confirmed = false;
    $("#ctl00_mainContent_lnkDisable").click(function() {
        if( confirmed ){ return true; }

        jConfirm('Please confirm this emp should be disabled?', 
                 'Confirm Disable', 
                 function(r) {
                            jAlert('Confirmed: ' + r, 'Results');
                            confirmed = true;
                            #("ctl00_mainContent_lnkDisable").click();
                            confirmed = false;
            });
        });

        return false;
    });

Untested, and fairly silly (I'm not sure if calling .click() will fire the event again, hence the boolean), but it should work.

Noon Silk
Thanks Silky... indeed the return sounds logical, and likely part of the end solution. In this case however, it did not stop the postback from occurring.
p.campbell
pcampbell: Make sense, I guess you need to determine how to make that dialog modal. It may be an option/parameter in that function.
Noon Silk
After a quick read of the site, it doesn't look like you can make them modal. That's a bit annoying. So what do you then is a little more complicated. I'll update the main post.
Noon Silk
A: 

I did following and it's works:

            var confirmed = false;
            $(".removeItemLink").click(function() {
                if (confirmed) { return true; }

                var clickedObj = $(this);
                jConfirm('Are you sure you want to remove this item?',
                 'Confirm Removal',
                 function(r) {
                     if (r) {
                         confirmed = true;
                         clickedObj.click();
                         confirmed = false;
                     }
                 });

                return confirmed;
            });

@Silky - thanks.

Krunal
@Krunal: thank you for the answer. Unfortunately it didn't work for me.
p.campbell
A: 

i have the same problem posted here but not yet resolved http://stackoverflow.com/questions/3680470/jquery-plugin-calling-jconfirm-from-asp-net-code-behind

Abu Hamzah
A: 

You could have a hidden button that you "click" using jquery. Maybe something like this?

$(".removeItemLink").click(function() {
  var clickedObj = $(this);
  jConfirm('Are you sure you want to remove this item?',
    'Confirm Removal',
    function(r) {
      if (r) {
       clickedObj.parent().find(".hidden-button-with-event").click();
      }
  });
});
Wally Mathieu