user175084
user175084

Reputation: 4630

javascript confirm value not working

i have a confirm and alert message box. both are getting displayed at the right time.

but in confirm box when i press cancel the applyaction_button_click is getting executed, which should not happen as i return false.

Similar is the case with alert box which returns false still applyaction_button_click is getting executed.

here is my code:

   protected void Page_Load(object sender, EventArgs e)
    {
                    ApplyAction_Button.Attributes.Add("onclick", "ShowConfirm();");
}

   protected void ApplyAction_Button_Click(object sender, EventArgs e)
    {
       // Action to be applied
    }

JS function:

    function ShowConfirm() {
      //check if checkbox is checked if is checked then display confirm message else display alert message
      var frm = document.forms['aspnetForm'];
      var flag = false;
      for (var i = 0; i < document.forms[0].length; i++) {
          if (document.forms[0].elements[i].id.indexOf('Select_CheckBox') != -1) {
              if (document.forms[0].elements[i].checked) {
                  flag = true
              }
          }
      }

      if (flag == true) {
          if (confirm("Are you sure you want to proceed?") == true) {
              return true;
          }
          else {
              return false;
          }
      } else {
      alert('Please select at least Checkbox.')
      return false;
      }
  }

thanks

Upvotes: 1

Views: 387

Answers (1)

Nick Rolando
Nick Rolando

Reputation: 26167

When you have a client side event on an asp server control that has a server event and both get triggered with the same action, returning true or false on the client doesn't stop it from executing its server event.

You could try to prevent the button's server event from executing by doing some kind of postback in your javascript code.

Upvotes: 1

Related Questions