OLDMONK OLDMONK - 7 months ago 120
Javascript Question

Passing An ASP.NET Button Click Event in SweetAlert

I have a C# method which performs a suspend operation.

protected void SuspendButton_OnClick(object sender, EventArgs e)
{
var accountNumberId = DepositAccount.DepositAccountNumberId;
var depositAccount = AccountHolders.GetAccountHolder(accountNumberId);

if (depositAccount == null)
{
ShowFailModal("No Account Selected");
}

Common.Deposit.AccountSuspension accntSuspension = new Common.Deposit.AccountSuspension();
accntSuspension.AuditTS = BusinessLayer.Core.DateConversion.GetCurrentServerDate();
accntSuspension.AuditUserId = UserId;
accntSuspension.Description = DescriptionTextBox.Text;
accntSuspension.SuspendedDate = GetDate;
accntSuspension.AccountNumberId = accountNumberId;

if (depositAccount != null)
{
InsertSuspendedAccount(accntSuspension);
}

}


I am using BootBox for the same now



$('#SuspendButton').on('click', function (evt) {
evt.preventDefault();
var message = "Are you sure you want to Suspend this Account?";

bootbox.confirm(message, function (result) {
if (result === false) {
evt.preventDefault();
} else {
// $.showprogress("Account Suspending.Please Wait...");
window.__doPostBack("<%= SuspendButton.UniqueID %>", "");
}

});
});





This is the sample of SweetAlert:



swal({ title: "Are you sure?",
text: "You will not be able to recover this imaginary file!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it!",
closeOnConfirm: false
},
function(){
swal("Deleted!", "Your imaginary file has been deleted.", "success");
});





How can i make it work like BootBox?Like in BootBox, when i press the SuspendButton, it throws a bootbox.confirm popup, and if i press O,K the underlying operation is performed.Can i do that same with SweetAlert?

Answer

You can try something like this, let me know if this is not what you want

$('#SuspendButton').on('click', function (evt) {
           evt.preventDefault();
           //var message = "Are you sure you want to Suspend this Account?";

       swal({   title: "Are you sure?",
            text: "You will not be able to recover this imaginary file!",
            type: "warning",   
            showCancelButton: true,   
            confirmButtonColor: "#DD6B55",   
            confirmButtonText: "Yes, delete it!",   
            closeOnConfirm: false 
            },
            function(isConfirm){   
            if (isConfirm) {     
                // $.showprogress("Account Suspending.Please Wait...");
                window.__doPostBack("<%= SuspendButton.UniqueID %>", "");  
            } else {     
            evt.preventDefault();   }
        });    
  });