简体   繁体   English

JavaScript通过ASP.NET确认

[英]JavaScript confirm with ASP.NET

I am using JavaScript confirm in an ASP.NET program, the confirm window works when I want it to, however I am not sure how to retrieve whether the user clicks 'OK' or 'Cancel'. 我在ASP.NET程序中使用JavaScript确认,确认窗口在我想要的时候工作,但是我不知道如何检索用户是否单击“确定”或“取消”。

This is in the site master: 这是在站点主站:

            <script type="text/javascript">

                    var confirmMsg = document.getElementById('MainContent_confirmMessageHidden');

                    if (confirmMsg != null) confirm(confirmMsg.value);

            </script>

This is in the aspx.cs file: 这是在aspx.cs文件中:

    private void Confirm(string msg)
    {
        //Response.Write("<script language = 'javascript'>window.alert('" + msg + "')</script>");
        confirmMessageHidden.Value = msg;
        confirmMessageHidden.Visible = true;


    }

How can I retrieve the choice of the user? 如何检索用户的选择?

You need to use return value of javascript confirm function. 你需要使用javascript确认函数的返回值。 For example, 例如,

...

if (confirmMsg != null) {
  var answer = confirm(confirmMsg.value);
  if (answer) {
    alert('OK Clicked');
  }
  else {
    alert('Cancel Clicked');
  }
}
...

Choose appropriate action instead of alerts as per the functionality needed by you. 根据您所需的功能选择适当的操作而不是警报。

EDIT: 编辑:

A sample code to prevent navigation using confirm 使用确认阻止导航的示例代码

<script type="text/javascript">
    function doConfirm() {
       var confirmMsg = document.getElementById('MainContent_confirmMessageHidden');
       if (confirmMsg != null) {
          return confirm(confirmMsg.value);
       }
       return true;
    }
</script>

<a href="link to some other page" onclick="return DoConfirm();" />

<input type="sumbit" value="Click Me" onclick="return DoConfirm();" />

You could use a conditional action based on confirm choose: 您可以使用基于确认选择的条件操作:

for example something like 比如说像

if (confirm(confirmMsg.value))
{
     document.location('<your_process_page_url.aspx>?confirm=1');
}
else
{
     document.location('<your_process_page_url.aspx>?confirm=0');
}

Then in process page you can retrieve the choose by 然后在流程页面中,您可以检索选择

<%
 var choose = Request.QueryString["confirm"];
%>

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

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