1

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 the choice of the user?

2 Answers 2

3

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();" />
Sign up to request clarification or add additional context in comments.

7 Comments

ok this works, how could i tell it to proceed if OK is clicked? (proceed being going to where the page was directed before the confirmation)
@Karl, if your script is invoked on submit button then you need to return true (on confirmation) on click event handler to proceed and return false to do nothing. The same will apply to a hyperlink (anchor element). See my edit.
when i try doing that, i create a function in the sitemaster but it simply doesnt do the confirm alert at all
but the problem is this is not a normal button, its a select button which is autogenerated, so im not sure how i can access its onClick
@Karl, what do you mean by select button? Do you mean a drop-down that does auto-postback (onchange will be correct event handler).
|
1

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"];
%>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.