开发者

how can a clicked asp.net button open jQuery dialog and then resume default when Yes is clicked?

I've got a jQuery dialog and an asp.net button (Delete). When the delete button is clicked I want the dialog to open a confirm window saying 'Are you sure you want to delete this record?'. When the user clicks yes I'd like to resume the asp.net postback. Here's my code so far:

   $("#dialog-delete").dialog({
        autoOpen: false,
        height: 200,
        width: 400,
        modal: true,
        resizable: false,
        buttons: {
            'Cancel': function () {
                $(this).dialog('close');
            },
            'Yes': function () {
                $(this).dialog('close');
                //__doPostBack('ctl00$cp1$btnDelete','');
            }
        },
        open: function () {
            $(":button:contains('Yes')").addClass("red");
        }
    });
     $("[id*=btnDelete]").live('click', function (e) {
        e.preventDefault();
        $("#dialog-delete").dialog('open');
     });

So I stop the postback with preventDefault the dialog opens and displays the messa开发者_如何学Cge but I can't get the delete to fire without hardcoding the delete button, which is nasty. Is there anyway I can hold up the click event whilst the dialog displays and call preventDefault on Cancel and resume on Yes?


You could change the live event type to mouseup instead of click and then use the click event to fire the postback.

$("[id*=btnDelete]").live('mouseup', function (e) { ...

...

$("[id*=btnDelete]").click();

Edit (based on comments)

Another thought, you could stick with the original idea of handling the click event and upon confirmation of the delete call the .die() function to remove the live event handler previously attached, enabling you to call .click() and causing a postback.

You can call the .die() function like so:

$("#dialog-delete").dialog({
    autoOpen: false,
    height: 200,
    width: 400,
    modal: true,
    resizable: false,
    buttons: {
        'Cancel': function () {
            $(this).dialog('close');
        },
        'Yes': function () {
            $(this).dialog('close');
            $("[id*=btnDelete]").die('click');
            $("[id*=btnDelete]").click();
        }
    },
    open: function () {
        $(":button:contains('Yes')").addClass("red");
    }
});

 $("[id*=btnDelete]").live('click', function (e) {
    e.preventDefault();
    $("#dialog-delete").dialog('open');
 });


Submit the ASP.NET form when the appropriate button is clicked.

...
buttons: {
      'Cancel': function () {
           $(this).dialog('close');
       },
       'Yes': function () {
           $(this).dialog('close');
           $('#aspnetForm').submit();   // you may need to use a different selector based on your form name
       }
   }


I got this working but I don't like the solution so far. I'd much prefer a cleaner solution where I can capture the event on the button click and pass it into my dialog in some way, if that's possible. So I'm holding out and hoping someone has a better answer. My code that got it working (for now) is:

$("#dialog-delete").dialog({
    autoOpen: false,
    height: 200,
    width: 400,
    modal: true,
    resizable: false,
    buttons: {
        'Cancel': function () {
            $(this).dialog('close');
        },
        'Yes': function () {
            $(this).dialog('close');
            $("[id*=btnDelete]")[0].click();
            //__doPostBack('ctl00$cp1$btnVoid','');
        }
    },
    open: function () {
        $(":button:contains('Yes')").addClass("red");
    }
});
 $("[id*=btnDelete]").live('mousedown', function (e) {
    e.preventDefault();
    $("#dialog-delete").dialog('open');
 });

Based on the help from @jdavies above (and my comments to enhance the answer). I changed my button handler to a mousedown event and fired the click event in the Yes button handler.

I'm wondering what will happen if the user fires the button via the keyboard, which is part of the reason this feels icky to me. :)

Holding out for something better but it's running at least...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜