admin管理员组

文章数量:1401794

I am trying to use the sweet alert to use as a pop-up message in my ASP.NET C# application. But I think I am doing wrong because, If I click the button or Link button, nothing really happens. it's just like an element without an event.

So here is the code.

JAVASCRIPT

<script src=".min.js"></script>
    <script type="text/javascript">
        function Confirm(ctl, event) {
            event.preventDefault();
            swal({
                title: "Confirm Logout?",
                text: "Do you really want to log this Account out?",
                type: "warning",
                showCancelButton: true,
                closeOnConfirm: true,
                closeOnCancel: true
            },
            function (isConfirm) {
                if (isConfirm) {
                    return true;
                } else {
                    return false;
                }
            });
        }
    </script>

ASPX

    <li class="nav-item">
  <asp:LinkButton ID="btnLogout" CssClass="nav-link" runat="server" OnClick="btnLogout_Click" OnClientClick="return Confirm(this,event)"><i class="icon ion-android-exit"></i></asp:LinkButton></li>

C#

 protected void btnLogout_Click(object sender, EventArgs e)
    {
         Session.Abandon();
         Response.Redirect("login.aspx");
    }

I am trying to use the sweet alert to use as a pop-up message in my ASP.NET C# application. But I think I am doing wrong because, If I click the button or Link button, nothing really happens. it's just like an element without an event.

So here is the code.

JAVASCRIPT

<script src="https://unpkg./sweetalert/dist/sweetalert.min.js"></script>
    <script type="text/javascript">
        function Confirm(ctl, event) {
            event.preventDefault();
            swal({
                title: "Confirm Logout?",
                text: "Do you really want to log this Account out?",
                type: "warning",
                showCancelButton: true,
                closeOnConfirm: true,
                closeOnCancel: true
            },
            function (isConfirm) {
                if (isConfirm) {
                    return true;
                } else {
                    return false;
                }
            });
        }
    </script>

ASPX

    <li class="nav-item">
  <asp:LinkButton ID="btnLogout" CssClass="nav-link" runat="server" OnClick="btnLogout_Click" OnClientClick="return Confirm(this,event)"><i class="icon ion-android-exit"></i></asp:LinkButton></li>

C#

 protected void btnLogout_Click(object sender, EventArgs e)
    {
         Session.Abandon();
         Response.Redirect("login.aspx");
    }
Share Improve this question edited Apr 11, 2019 at 7:24 Stephan Bauer 9,2375 gold badges38 silver badges59 bronze badges asked Apr 11, 2019 at 6:05 Boooo BoooooBoooo Booooo 551 gold badge2 silver badges11 bronze badges 2
  • 2 Sweet alert work asynchronously, so your confirm function will always returns false as written, But why sweet alert itself is not displayed, I'm not sure, Is there any error in the browser console? – Mat J Commented Apr 11, 2019 at 6:18
  • 1 @MatJ , It does not have an error sir. but you are right. the message is not shown. – Boooo Booooo Commented Apr 11, 2019 at 6:20
Add a ment  | 

1 Answer 1

Reset to default 3

OnClientClick waits bool result to invoke server event but Confirm methods return nothing. ( function (isConfirm) returns async.)

You could call server event on function (isConfirm) manually like

<script type="text/javascript">
    function Confirm(ctl, event) {
        event.preventDefault();
        swal({
            title: "Confirm Logout?",
            text: "Do you really want to log this Account out?",
            type: "warning",
            showCancelButton: true,
            closeOnConfirm: true,
            closeOnCancel: true
        },
        function (isConfirm) {
            if (isConfirm) {
               _doPostBack('btnLogout', 'OnClick');
            } 
        });

       return false;
    }
</script>

本文标签: javascriptHow to use Sweet Alert in ASPNET CStack Overflow