I try to use sweetalert2 when deleting gridview a row.
This my js:
<script>
var object = { status: false, ele: null };
function ConfirmDelete(ev) {
if (object.status) { return true; };
Swal.fire({
title: 'Are you sure?',
text: "You won't be able to revert this!",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!',
preConfirm: function () {
return new Promise(function (resolve) {
setTimeout(function () {
resolve()
}, 2000)
})
object.status = true;
object.ele = ev;
object.ele.click();
}
}).then(function () {
})
return false;
};
</script>
<asp:ImageButton ImageUrl="~/dist/img/delete.png" runat="server" CommandName="Delete" OnClientClick="return ConfirmDelete();" ToolTip="Sil" Width="20px" Height="20px" />
2
Answers
I solved my problem codes below.
This is button
This is JS
Set this up to be more simple. You want that function to return true, or false.
And introduction of a promise and hand stands will not solve this problem, since like most js add-ins, be it jQuery.UI dialogs, or this case sweet alerts, they run asynchronous – and introduction of promise will NOT help you unless that alert routine is being called by other js routines – and it is not.
Also, we "assume" that the button you have is in the grid – looks like it is, but you should have at least provide the surrounding markup.
Not the end of the world.
So, this should work:
Your button, becomes this:
Note VERY careful in above we PASS the button to the js routine.
so, the js can now become this:
Note the last line of the routine "return false"
And I don’t see ANY reason for creating some js object to return when ALL we care about is returning true or false – so use a simple variable for that.