如何在ASP.NET C#中使用Sweet Alert

3

我正在尝试在我的ASP.NET C#应用程序中使用Sweet Alert作为弹出消息。但我认为我做错了什么,因为如果我点击按钮或链接按钮,什么都不会发生。就像没有事件的元素一样。

这是代码:

JAVASCRIPT

<script src="https://unpkg.com/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");
    }

2
Sweet Alert 是异步工作的,因此您的确认函数将始终按照编写的方式返回 false。但是为什么 Sweet Alert 本身没有显示,我不确定。浏览器控制台中是否有任何错误? - Mat J
1
@MatJ ,先生,它没有错误。但您是正确的。消息未显示。 - Boooo Booooo
1个回答

3

OnClientClick等待一个bool类型的结果来触发服务器事件,但Confirm方法并没有返回任何东西。(function (isConfirm) 是异步的。)

你可以像下面这样手动调用 function (isConfirm) 来触发服务器事件:

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

甜蜜警告消息没有显示,但它成功销毁了会话。 - Boooo Booooo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接