开发者

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

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:

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


    }

How can I retrieve 开发者_JAVA技巧the choice of the user?


You need to use return value of javascript confirm function. 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"];
%>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜