简体   繁体   English

从背后的代码触发JavaScript Confirm()

[英]Trigger JavaScript Confirm() from code behind

I am working on a Asp.Net application, currently i am trying to trigger a JavaScript Confirm() popup from the code behind. 我正在使用Asp.Net应用程序,当前正在尝试从后面的代码触发JavaScript Confirm()弹出窗口。 I would like to popup without clicking any button_click event. 我想弹出而不单击任何button_click事件。

IF not blnResult then

popup message with OK& CANCEL

IF OK THEN Exit Sub

ELSE

no display

END IF 

I tried to do below things and it's not firing popup, please assist. 我尝试执行以下操作,但未触发弹出窗口,请提供帮助。

1) Created a button in ASPX 1)在ASPX中创建一个按钮

<asp:Button ID="btnConfirm" runat="server" Text="Delete-All" OnClick="btnConfirm_Click" OnClientClick="Confirm()"/>

JavaScript function JavaScript功能

<script language="javascript" type = "text/javascript">
    function Confirm() {
        var confirm_value1 = document.createElement("INPUT");
        confirm_value1.type = "hidden";
        confirm_value1.name = "confirm_value";
        if (confirm("Do you want to delete all records?")) {
            confirm_value1.value = "Yes";
        } else {
            confirm_value1.value = "No";
        }
        document.forms[0].appendChild(confirm_value1);
    }
</script>

Code behind, 代码后面,

Public function GetConfirmation()

btnConfirm_Click(btnConfirm, EventArgs.Empty)

End Sub

Above line isn't firing the popup for me. 上面的行没有为我触发弹出窗口。

Just modify your javascript function. 只需修改您的javascript函数即可。

<script language="javascript" type = "text/javascript">
    function Confirm() {
        var confirm_value1 = document.createElement("INPUT");
        confirm_value1.type = "hidden";
        confirm_value1.name = "confirm_value";
        if (confirm("Do you want to delete all records?")) {
            confirm_value1.value = "Yes";
        } else {
            confirm_value1.value = "No";
        }
        document.forms[0].appendChild(confirm_value1);

        return false;//this will prevent submit click.
    } </script>

just add return statement at last line of function. 只需在函数的最后一行添加return语句。

return false; 返回false;

I hope this solution will help you. 希望此解决方案对您有所帮助。

If I have understood correctly then probably you want to call your code behind click event only when user confirms by click on yes. 如果我理解正确,那么您可能只想在用户通过单击“是”进行确认时才在click事件后调用代码。 There are many of ways doing this. 有很多方法可以做到这一点。 I have listed few of them. 我只列出了一些。 Choose which ever suits best for you. 选择最适合您的。

Don't know why you have created a hidden input field. 不知道为什么创建了隐藏的输入字段。 What is the purpose of creating it. 创建它的目的是什么。 In case you don't need hidden input filed you can try these out. 如果您不需要隐藏的输入字段,可以尝试一下。

  1. Confirmation in HTML Tag HTML标签中的确认
<asp:Button ID="btnConfirm" runat="server" Text="Delete-All" OnClick="btnConfirm_Click" OnClientClick="return confirm('Do you want to delete all records?');"/>
  1. Confirmation in JavaScript JavaScript确认
<asp:Button ID="btnConfirm" runat="server" Text="Delete-All" OnClick="btnConfirm_Click" OnClientClick="Confirm();"/>

<script language="javascript" type="text/javascript">
    function Confirm() {
        return confirm("Do you want to delete all records?");
    }
</script>

Or 要么

<script language="javascript" type="text/javascript">
    function Confirm() {
        var result = confirm("Do you want to delete all records?");    
        return result;
    }
</script>

If you do need to keep hidden input field then use below. 如果确实需要保留隐藏的输入字段,请在下面使用。

<script language="javascript" type="text/javascript">
    function Confirm() {
        var confirm_value1 = document.createElement("INPUT");
        confirm_value1.type = "hidden";
        confirm_value1.name = "confirm_value";

        var result = confirm("Do you want to delete all records?");    
        confirm_value1.value = result ? "Yes" : "No";    
        document.forms[0].appendChild(confirm_value1);    
        return result;    
    }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM