基于JS MessageBox执行OnServerClick是/否选择

Executing OnServerClick Based On JS MessageBox Yes/No Selection

本文关键字:选择 OnServerClick JS MessageBox 执行 基于      更新时间:2023-09-26

我正在使用带有ASP.Net.的Bootstrap

我正在尝试将确认添加到我的按钮中,以便只有在显示警报消息框后单击时,才会执行代码隐藏

<link href="_JS/jquery.modal.css" type="text/css" rel="stylesheet" />
<link href="_JS/jquery.modal.theme-xenon.css" type="text/css" rel="stylesheet" />
<link href="_JS/jquery.modal.theme-atlant.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="_JS/jquery.modal.min.js"></script>
<!--......-->
<a href="#" runat="server" id="confirm" onserverclick="confirm_ServerClick1">Confirm</a>
<!--......-->
<script type="text/javascript">
        $(document).ready(function (e) {
            $('a#confirm').click(function () {
                modal({
                    type: 'confirm',
                    title: 'Confirm',
                    text: 'Are you sure you want to remove the user from the company?',
                    if (result) {
                        //Proceeds to activate event in code behind - confirm_ServerClick1
                    } else {
                        BootstrapDialog.closeAll();
                    }}
                });
            });
        });
</script>

有可能吗?如果是的话,我将非常感谢任何提示。

到目前为止,它只是忽略JS函数执行confirm_ServerClick1事件,而不显示任何模式弹出窗口。

您需要在JS函数中返回true或false。False表示停止执行,true表示继续到服务器。

   $('#confirm').click( function() { your_code_here; return false; // or true. the result from your popup } );
     $(document).ready(function (e) {
            $('a#confirm').click(function () {
                modal({
                    type: 'confirm',
                    title: 'Confirm',
                    text: 'Are you sure you want to remove the user from the company?',
                    if (result) {
//your logic here
                     return true;
                    } else {
                        BootstrapDialog.closeAll(); return false;
                    }}
                });
            });
        });

编辑

请参阅下面的示例以获取默认的JS confirm。(JS Confirm将阻止执行,直到收到对话框的结果为止)。在您的情况下,引导程序弹出窗口不会阻止执行。您需要根据从弹出窗口收到的结果从JS调用服务器端事件。如果结果为true,则调用服务器端方法,如document.getElementById("CliendId").click()或使用__doPostBack

    <a href="#" runat="server" id="confirm" onclick="return confirmEvent();"  onserverclick="confirm_ServerClick1">Confirm</a>

<!--......-->
<script type="text/javascript">
    function confirmEvent()
    {
        if(confirm("Are you sure?"))
        {
            alert("This will execute server side event");
            return true;
        }
        else
        {
            alert("I'm not sure!");
            return false;
        }
    }
</script>