sweet alert confirm dialog in asp.net

2020-05-09 17:27发布

问题:

I am able to get the alerts for isConfirm both true and false in sweet alert but unable to pass the same to btnSave to call btnSave_Click method in codebehind. Please help. Using latest sweetalert, downloaded from http://t4t5.github.io/sweetalert/ .

<script type="text/javascript">
        $('body').on('focus', ".Datetext", function () {
            $(this).datepicker();
        });

        function Confirm(th, e) {
              e.preventDefault();
            swal({
                title: "Are you sure?",
                text: "You want to Save this project?",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: '#DD6B55',
                confirmButtonText: 'Yes, I am sure!',
                cancelButtonText: "No, cancel it!",
                closeOnConfirm: true,
                closeOnCancel: true
            },
            function (isConfirm) {
                if (isConfirm) {
                    //return true; 
                     alert("true");
                   // $(this).trigger('click');
                    $("#<%= btnSave.ClientID %>").trigger('click');
                   // e.currentTarget.submit();
                } else {
                    //  return false; 
                    // alert("False");
                }
            });
        } 
    </script> 

html

<div style="text-align: center;">
                <asp:Button ID="btnSave" CssClass="buttons___" runat="server" Font-Size="9pt" Text="Save Project" 
                    OnClick="btnSave_Click" OnClientClick="Confirm(this, event);"/>
                <asp:Button CssClass="buttons___" runat="server" Font-Size="9pt" Text="Save as Draft" />
            </div>

回答1:

Please try below changes in your code

<asp:Button ID="btnSave" CssClass="buttons___" runat="server" Font-Size="9pt" Text="Save Project" 
                    OnClick="btnSave_Click" OnClientClick="return Confirm(this, event);"/>

and in Script of sweet alert

function (isConfirm) {
                if (isConfirm) {
                    return true; 
                    // alert("true");
                   // $(this).trigger('click');
                    $("#<%= btnSave.ClientID %>").trigger('click');
                   // e.currentTarget.submit();
                } else {
                      return false; 
                    // alert("False");
                }
            }