Bootbox confirm: return client result in order to do postback to rowCommand

社会主义新天地 提交于 2019-12-21 02:50:09

问题


Before bootbox, I did this on aspx file in gridview;

<asp:Button ID="btnDelete" CssClass="btn btn-danger" OnClientClick="if(!confirmDelete()) return false;" runat="server" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" CausesValidation="false" CommandName="DeleteRow" Text="Delete"/>

and on js file;

function confirmDelete() {
return confirm("Are you sure you want to delete the record?"); }

And with confirmation, the gridview's RowCommand is triggered and the deletion is done.

With bootbox, I am realy stuck. I know bootbox is asynchronus and try to use 'preventDefault', but it didn't work. So how can I convert the above js file to bootbox version? Thanks in advance.


回答1:


I finally come up with this solution;

function confirmDelete(sender) {
    if ($(sender).attr("confirmed") == "true") {return true;}

    bootbox.confirm("Are you sure you want to delete?", function (confirmed) {
        if (confirmed) {
            $(sender).attr("confirmed", confirmed).trigger("click");
        }
    });

return false;
}

And changing button's OnClientClick;

OnClientClick="return confirmDelete(this);"



回答2:


I've tried Fatih Bilginer solution, but it requires another click to do postback, so I changed .trigger("click") for sender.click();

EDIT

function confirmDelete(sender) {
    if ($(sender).attr("confirmed") == "true") {return true;}

    bootbox.confirm("Are you sure you want to delete?", function (confirmed) {
        if (confirmed) {
            $(sender).attr('confirmed', confirmed);
            sender.click();
        }
    });

return false;
}



回答3:


I've doing a some change in dannyzar code, I used callback: to call function

and I've added some design to bootbox modal

function confirmDelete(sender) {
          if ($(sender).attr("confirmed") == "true") { return true; }

          bootbox.confirm({
          message: "Are you sure you want to delete?",
          buttons: {
              confirm: {
                  label: 'Yes',
                  className: 'btn btn-success'
              },
              cancel: {
                  label: 'No',
                  className: 'btn btn-danger'
              }
          }
          ,callback: function (confirmed) {
              if (confirmed) {
                  $(sender).attr('confirmed', confirmed);
                  sender.click();
              }
          }});

          return false;
      }


来源:https://stackoverflow.com/questions/30912017/bootbox-confirm-return-client-result-in-order-to-do-postback-to-rowcommand

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!