hope someone can help me understand.. I have an asp.net project for which I have decided to use SweetAlert for a confirmation button. I have followed suggestions in here:
Using SweetAlert2 to replace "return confirm()" on an ASP.Net Button
The issue is that although the SweetAlert prompt is working fine, when accepting, the server side code is not being executed.
<script src="Scripts/sweetalert-dev.js"></script>
<link href="Styles/sweetalert.css" rel="stylesheet" />
this is the button:
<asp:Button ID="btndiscard" runat="server" Font-Size="Large" Text="Discard" Width="100px" onClientClick="return fnConfirmDiscard(this);" OnClick="btndiscard_Click" ToolTip="Discard this application (must be pending)"/>
this is the javascript code:
<script type="text/javascript">
var myok = false
function fnConfirmDiscard(btndiscard) {
//return confirm("Confirm to discard this application");
if (btndiscard.dataset.confirmed) {
// The action was already confirmed by the user, proceed with server event
btndiscard.dataset.confirmed = false;
alert("true");
return true;
} else {
// Ask the user to confirm/cancel the action
event.preventDefault();
swal({
title: 'Are you sure?',
text: "You won't be able to revert this!",
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
}).then(function(myresult) {
// Set data-confirmed attribute to indicate that the action was confirmed
if (myresult) {
btndiscard.dataset.confirmed = true;
alert("test then");
// Trigger button click programmatically
btndiscard.click();
}
}).catch(function (reason) {
// The action was canceled by the user
alert("test catch");
});
}
}
and this is the server side codebehind:
protected void btndiscard_Click(object sender, EventArgs e)
{
// this procedure will discard a credit application (change status to "X")
// ask for confirmation
bool wasposted = false;
try
{
System.Data.SqlClient.SqlCommand qryproc;
string CAccountsConnection = System.Configuration.ConfigurationManager.ConnectionStrings["CAConnectionString"].ConnectionString;
string entid = ("00000" + lblentid.Text.Trim());
qryproc = new System.Data.SqlClient.SqlCommand("exec sp_CAChangeStatus @entid,@newstatus");
qryproc.Parameters.Add("@entid", SqlDbType.Char, 11);
qryproc.Parameters.Add("@newstatus", SqlDbType.Char, 1);
qryproc.Parameters["@entid"].Value = entid;
qryproc.Parameters["@newstatus"].Value = "X";
qryproc.Connection = new System.Data.SqlClient.SqlConnection(CAccountsConnection);
qryproc.Connection.Open();
qryproc.ExecuteNonQuery();
wasposted = true;
qryproc.Connection.Close();
qryproc.Dispose();
wasposted = true;
}
catch
{
wasposted = false;
}
if (wasposted)
{
lblcurrstatus.Text = "D";
MessageToUser("Transaction successfull, application discarted.",MessageTypes.result_ok);
ResetAllFields();
lblentid.Text = "";
btnnewpage.Text = "New";
MultiView1.ActiveViewIndex = 0;
}
else
MessageToUser("Transaction not successfull! please try again.",MessageTypes.error);
}
before using sweetalert I was using a simple javascript confirm box and that was working without issues and the server side procedure ran without problems.
if I remove event.preventDefault() then the server side procedure is called BUT the sweetalert cancel does not work either (the server side code is executed anyway)
Anybody has seen this issue? Any help would be greatly appreciated, Thanks.