Hi nauna,
You can't directly call the server side code from Sweet Alert confirm.
If you want to perform some task on server side on the confirm button click of Sweet Alert then you need to make ajax call and acheive your task.
So you need to use call back function in which ajax request needs to be made.
Refer below sample.
HTML
<asp:LinkButton ID="lnkdiscard" runat="server" CssClass="pull-right btn l-grey-bg grey-c"
OnClientClick="return confirm()">Discard</asp:LinkButton>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<script type="text/javascript">
function confirm() {
swal({
title: "Are you sure you want to Discard this listing?",
text: "Once discard, data will not save!",
icon: "warning",
buttons: true,
dangerMode: true
}).then(function (isConfirm) {
if (isConfirm) {
$.ajax({
type: "POST",
url: "Default.aspx/DeleteClick",
data: {},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
if (r.d != "") {
swal(r.d, 'success');
}
}
});
}
});
return false;
}
</script>
Code
C#
[System.Web.Services.WebMethod]
public static string DeleteClick()
{
return "Changes not saved.";
}
VB.Net
<System.Web.Services.WebMethod>
Public Shared Function DeleteClick() As String
Return "Changes not saved."
End Function
Screenshot