2

I'd like to use jQuery UI's dialog to implement a confirm dialog which is shown when the user clicks a delete-link (implemented using an asp:LinkButton).
I'm using code as shown below (copied from the jquery ui documentation):

<!-- the delete link -->
<asp:LinkButton ID="btnDelete" runat="server" Text="Delete"
  OnClick="btnDelete_Click" CssClass="btnDelete"></asp:LinkButton>

<!-- the confirm-dialog -->
<div id="dialog-confirm-delete" title="Delete?" style="display:none;">
  <p>Are you sure you want to permanently deleted the selected items?</p>
</div>

<script>
$(document).ready(function () {
    // setup the dialog
    $('#dialog-confirm-delete').dialog({
        autoOpen: false,
        modal: true,
        buttons: {
          "Delete all items": function () {
                 $(this).dialog("close");
                 // ===>>> how to invoke the default action here
              },
          Cancel: function () { $(this).dialog("close"); }
        }
    });

    // display the dialog
    $('.btnDelete').click(function () {
        $('#dialog-confirm-cancel').dialog('open');
        // return false to prevent the default action (postback)
        return false;
    });

});
</script>

So in the click event handler, I have to prevent the default action of the LinkButton (the postback) and instead display the dialog.

My question is: how can I then invoke the default action (the postback) of the delete link to perform the postback in case the user clicked the "Delete all items" button in the dialog?

7 Answers 7

6

OK, here's my approach (it works, but it might not be the best solution):

$(document).ready(function () {
  $('#dialog-confirm-cancel').dialog({
    autoOpen: false,
    modal: true,
    buttons: {
      "Delete all items": function () {
        // invoke the href (postback) of the linkbutton,
        // that triggered the confirm-dialog
        eval($(this).dialog('option', 'onOk'));
        $(this).dialog("close");
      },
      Cancel: function () { $(this).dialog("close"); }
    }
  });

  $('.btnDelete').click(function () {
    $('#dialog-confirm-delete')
      // pass the value of the LinkButton's href to the dialog
      .dialog('option', 'onOk', $(this).attr('href'))
      .dialog('open');
    // prevent the default action, e.g., following a link
    return false;
  });
});
3
  • What does eval()ing the href do? Is the href not a URL? Also, did my edited answer not work? Nov 9, 2010 at 19:07
  • @Drackir: It's not a path to a file. I want to invoke the PostBack of the LinkButton, which is a javascript function call, e.g. __doPostback(...)
    – M4N
    Dec 9, 2010 at 21:12
  • @Drackir: yes, exactly (that's the way how postbacks work in ASP.NET)
    – M4N
    Dec 10, 2010 at 9:03
1

If you're not doing anything more than confirming you can add an attribute to the button.

<asp:LinkButton ID="btnDelete" runat="server" Text="Delete"
  OnClientClick="if(!confirm('Are you sure?'))return false;" CssClass="btnDelete"></asp:LinkButton>
3
  • 3
    Or better yet return confirm("Are you sure?");. However, M4N said that they are using jQuery Dialogs for this and so they probably want to keep a coherent feel for all message boxes/pop-ups. Nov 9, 2010 at 16:44
  • lol i couldnt remember what it was, I havent used webforms in a couple years. I guess you can do that on action links as well, but yea its understandable if you're keeping a unified look. It just seems like a lot of work for a confirm dailog. Nov 9, 2010 at 17:47
  • Thanks, but it's not a simple confirm dialog. The dialog also contains other elements (textboxes, etc.).
    – M4N
    Nov 10, 2010 at 7:49
0

If you look at the Project Awesome on Codeplex it has a generic implementation of a Confirm Dialog that you can inspect for your scope.

0

So you prevented the default action of the link(following the link), right? So adding location.replace('path/to/file'); after $(this).dialog('close'); would solve your problem.

Not sure I understood your question right though.

1
  • I don't have a path to a file. I want to invoke the PostBack of the LinkButton (which is a javascript function call: __doPostback(...))
    – M4N
    Nov 9, 2010 at 15:31
0

Try adding $("#yourformid").submit(); at this spot // ===>>> how to invoke the default action here. According to the docs: "... the default submit action on the form will be fired, so the form will be submitted."

Edit
You can try to do something like this:

$('.btnDelete').click(function (event, confirmed) {
    if (confirmed) {
        return true;
    } else {
        $('#dialog-confirm-cancel').dialog('open');
        // prevent the default action, e.g., following a link
        return false;
    }
});

And then in your delete all items function:

"Delete all items": function () {
    $(this).dialog("close");
    $('.btnDelete').trigger("click", [true]);
},
4
  • I don't think this will work. I have to invoke the LinkButton's PostBack.
    – M4N
    Nov 9, 2010 at 15:32
  • @M4N - Have you tried calling the .click() function on the button ID? Ex: $("#buttonid").click(); You may need to get the button's ClientID at runtime for this to be sure Asp.Net doesn't screw up the name. I don't have Asp.Net here but it should be something like this: $("#<%= btnDelete.ClientID %>").click(); Nov 9, 2010 at 15:39
  • Wouldn't that trigger the click event handler once again?
    – M4N
    Nov 9, 2010 at 16:17
  • @M4N - Good point. What if you stored a global variable as to whether or not it had been confirmed and if it has, ignore the dialog open and return true? I also have another idea that doesn't require a global variable which I will edit into my answer. Nov 9, 2010 at 16:30
0
$('#dialog-confirm-delete').dialog({
   autoOpen: false,
   modal: true,
   buttons: {
       Cancel: function() {
           $(this).dialog("close");
       },
       "Delete all items": function() {
           $(this).dialog("close");
           // ===>>> how to invoke the default action here
       }
   }
});
0

If you are using a LinkButton you can do this:

__doPostBack("<%= lnkMyButton.UniqueID %>", "");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.