Search code examples
c#asp.netvisual-studiogridviewsweetalert2

SweetAlert2 ASP.NET Gridview Delete Row Confirm


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" />


Solution

  • I solved my problem codes below.

    This is button

    <asp:ImageButton ImageUrl="~/dist/img/delete.png" runat="server" CommandName="Delete" OnClientClick="return conFunction(this);" ToolTip="Sil" Width="20px" Height="20px" />
    

    This is JS

    <script type="text/javascript">
        var object = { status: false, ele: null };
        function conFunction(ev) {
            var evnt = ev;
            if (object.status) { return true; }
            Swal.fire({
                title: 'Silmek istediğinize emin misiniz?',
                text: "Onayladıktan sonra işlem geri alınamaz",
                icon: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#3085d6',
                cancelButtonColor: '#d33',
                confirmButtonText: 'Evet, Sil',
                cancelButtonText: 'İptal',
                closeOnConfirm: true
            }).then((result) => {
                if (result.isConfirmed) {
                    object.status = true;
                    object.ele = evnt;
                    evnt.click();
                }
            })
    
            return object.status;
        };
    </script>