Akshay
Akshay

Reputation: 1472

sweet alert confirm dialog in asp.net

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>

Upvotes: 0

Views: 6274

Answers (2)

Hafizur Rahman
Hafizur Rahman

Reputation: 11

I think the you are looking for this:

        var object = { status: false, ele: null };
        function conFunction(ev) {
            var evnt = ev;
            if (object.status) { return true; }
            Swal.fire({
                title: 'Are you sure?',
                text: "You won't be able to modify the form once submitted",
                icon: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#3085d6',
                cancelButtonColor: '#d33',
                confirmButtonText: 'Yes, Submit'
                
            }).then((result) => {
                if (result.isConfirmed) {
                    object.status = true;
                    object.ele = evnt;
                    evnt.click();
                    Swal.fire('Submitted');
                }
            })

            return object.status;
        };
    
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="//cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<input type="button" id="Button3" runat="server" value="Submit" onclick="return conFunction(this);"/>

Upvotes: 1

Kevin Shah
Kevin Shah

Reputation: 1617

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");
                }
            }

Upvotes: 0

Related Questions