1

I have created a confirmation box that works as expected and return true/false on the button click. But it is a general confirm where I cannot set a custom title.

function Validate() {

    if ($('#cphBody_gvBins').find("input[value='Edit']").length > 0 || $('#cphBody_gvBins').find("input[value='Update']").length > 0 ) {
        var mConfirm = confirm("The Record contains data that will be deleted. Do you still want to proceed?");
        return mConfirm;
    }
}

I call it on a client event. The function returns true or false.

<asp:Button ID="btnIssuerRemove" runat="server" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" 
    CausesValidation="false" CommandName="Remove" Text="Remove" OnCommand="issuerCommand_Click" OnClientClick="return Validate()"/>

But, it is just a regular confirmation box.

So, I went ahead and created a div:

<div id="dialogBox">
    Are you sure?
</div> 

And then I changes the function to display my div as a dialog:

function CheckForBins() {

    if ($('#cphBody_gvBins').find("input[value='Edit']").length > 0 || $('#cphBody_gvBins').find("input[value='Update']").length > 0) {
        //var mConfirm = confirm("The issuer contains Bins that will be deleted. Do you still want to proceed?");
        $("#dialogBox").dialog({
            title: "System Message",
            modal: true,
            resizable: false,
            width: 250,
            buttons: {
                Cancel: function () {
                    $(this).dialog('close');    
                },
                OK: function(){
                    $(this).dialog('close');
                }
            }
        });

        return false;
    }
}

Now, with that set up, when I click the "Remove" button, dialog is displayed. However, it does not do anything on "OK"

How can I return true/false from here, so, I do not delete the record when "Cancel" is pressed and "Delete" when "OK" is pressed.

gene
  • 2,098
  • 7
  • 40
  • 98

1 Answers1

1

You didn't post your full HTML, so I took some liberties in creating some HTML content using your ID's provided in your example. Next time, please post your full HTML so we can see exactly what you're trying to achieve. Also, it looks like you are using jQuery and jQuery UI Dialog, even though you didn't specifically show us/state this.

Below is an example with a test record with the 3 buttons you identify in your JS. Upon clicking the Remove button, your IF statement checks for the Edit/Update buttons existing, and then allows the confirmation dialog to trigger.

Please see further documentation of UI Dialog here: https://jqueryui.com/dialog/#modal-confirmation

function Validate(thisRecordRow) {
  if ($('#cphBody_gvBins').find("input[value='Edit']").length > 0 || $('#cphBody_gvBins').find("input[value='Update']").length > 0) {

    var tableRow = $(thisRecordRow).parent('td').parent('tr');

    /*
    Logic without Defer
    */
    CheckForBinsNoDefer(tableRow);

    /* DEFER LOGIC COMMENTEND OUT AS IT WONT WORK FOR YOUR JQUERY VERSION
      CheckForBinsDefer(tableRow).then(function(answer) {
        console.log(answer); // remove me
        return answer;
      });
      */
  }
}

function DoDeleteFunction(tableRow, deleteRow) {
  console.log(deleteRow); // remove me
  if (deleteRow) {
    // do delete logic
    // example:
    $(tableRow).remove();
  } else {
    // do nothing
  }

}

function CheckForBinsNoDefer(tableRow) {
  $("#dialogBox").dialog({
    title: "Delete Record",
    modal: true,
    resizable: false,
    width: 400,
    buttons: {
      "Ok": function() {
        // call DoDeleteFunction with true;
        DoDeleteFunction(tableRow, true);
        $(this).dialog("close");
      },
      "Cancel": function() {
        // call DoDeleteFunction with false;
        DoDeleteFunction(tableRow, false);
        $(this).dialog("close");
      }
    }
  });
}

function CheckForBinsDefer(tableRow) {
  var defer = $.Deferred();
  $("#dialogBox").dialog({
    title: "Delete Record",
    modal: true,
    resizable: false,
    width: 400,
    buttons: {
      "Ok": function() {
        defer.resolve(true);
        $(this).dialog("close");
      },
      "Cancel": function() {
        defer.resolve(false);
        $(this).dialog("close");
      }
    }
  });
  return defer.promise();
}
#dialogBox {
  display: none;
}
<html>

<head>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>

<body>
  <div id="cphBody_gvBins">
    <div id="dialogBox">
      Are you sure?
    </div>
    <table>
      <tr id="1">
        <td>
          TEST RECORD 1
        </td>
        <td>
          <input type="button" value="Edit" />
        </td>
        <td>
          <input type="button" value="Update" />
        </td>
        <td>
          <input type="button" class="btnIssuerRemove" value="Remove" onclick="Validate(this);" />
        </td>
      </tr>
      <tr id="2">
        <td>
          TEST RECORD 2
        </td>
        <td>
          <input type="button" value="Edit" />
        </td>
        <td>
          <input type="button" value="Update" />
        </td>
        <td>
          <input type="button" class="btnIssuerRemove" value="Remove" onclick="Validate(this);" />
        </td>
      </tr>
      <tr id="3">
        <td>
          TEST RECORD 3
        </td>
        <td>
          <input type="button" value="Edit" />
        </td>
        <td>
          <input type="button" value="Update" />
        </td>
        <td>
          <input type="button" class="btnIssuerRemove" value="Remove" onclick="Validate(this);" />
        </td>
      </tr>
    </table>
  </div>
</body>

</html>
Woodrow
  • 2,740
  • 1
  • 14
  • 18
  • 1
    When "OK" is clicked, I call a method in code-behind that removes items. I showed it in my example in the button control. – gene Apr 19 '19 at 17:15
  • Sorry if I missed that. I see that you were using a JS confirm box. You can change the "Delete All Items" text to read "OK" for the jQuery Dialog. If you don't wish to use jQuery dialog, you can continue using the JS confirm box and just wrap the if statement like in the example shown here when you click OK it does OK action, or Cancel closes dialog and dosen't do action: https://www.w3schools.com/js/tryit.asp?filename=tryjs_confirm Please let me know if I can help further. – Woodrow Apr 19 '19 at 17:19
  • I do not want to use JS confirm. I had it before. The reason I do not want to use it is it does not allow me to have a custom title – gene Apr 19 '19 at 17:22
  • 10-4. Again, please let me know if you need more help with the example I posted or need assistance adapting it in a different manner. Thanks! – Woodrow Apr 19 '19 at 17:23
  • You example is the same what I have already and it does not work – gene Apr 19 '19 at 18:44
  • I don't see how it is the same as what you have already. Have you run the code snippet above? I don't see what you mean about it not working.. – Woodrow Apr 19 '19 at 18:45
  • It does not do anything on "OK" – gene Apr 19 '19 at 18:46
  • I've updated the code so the dialog buttons are "Ok" and "Cancel". When you click "Ok", it removes the table row from the table, simulating the record being deleted... As commented in the JS code, this is where you would put logic to do whatever you need to delete the record (XHR call to server to delete from DB, etc.) – Woodrow Apr 19 '19 at 18:51
  • I do not want to perform remove logic in my dialog. I want Validate function to return `true` or `false`. So, my `OnClientClick="return Validate()` returns `true` or `false` to decide if I need to call server-side Remove method or not – gene Apr 19 '19 at 19:01
  • I've updated the code snippet. Now when you click Remove button, it calls Validate(), which then calls the CheckForBins() dialog script and clicking a dialog button gets the deferred boolean value so you can return it inside the Validate() function. This is exactly what `@DanieleAlessandra` above said was already answered under another post. – Woodrow Apr 19 '19 at 19:14
  • I tried with your fix, and I have a javascript error: `Object doesn't support property or method 'Deferred'` – gene Apr 19 '19 at 19:23
  • what version of jQuery are you using? Deferred is supported 1.5 and up, https://api.jquery.com/category/deferred-object/ – Woodrow Apr 19 '19 at 19:39
  • That's explain it. We use 1.4.2 version. Is there any work around it? We do not want to upgrade the jquery version yet – gene Apr 19 '19 at 20:15
  • Instead of expecting a boolean back from your validate function, you could use what I had earlier, where the logic inside OK and Cancel inside the dialog triggers a callback to a function and pass true/false to whatever logic you're trying to use for removing/deleting a record. – Woodrow Apr 19 '19 at 20:22
  • I'm aware it's not working for you due to older version of JS; I meant you could use my original answer without Defer and add a callback function. Please see updated snippet... – Woodrow Apr 19 '19 at 20:36
  • Again, your logic does not return the boolean from the javascript function that is defined for `OnClientClick="return ValidateBinExists()` What you are trying to do is to call remove functionality directly on a button's action. I do not want to do it. I just want to return a boolean. DoDeleteFunction is not supposed to return boolean, ValidateBinExists should. Currently, the popup is displayed, but I cannot click on any buttons, the function is returned back to the onClientClick – gene Apr 19 '19 at 20:46
  • Ok. Good luck. I feel I've helped about as much as I can and have provided several different ways of doing things. Please update this question with whatever answer you end up finding that works for you. Thanks. Have a good weekend. – Woodrow Apr 19 '19 at 20:49
  • @gene did you ever resolve this? I thought of another option that would allow you to use `Defer` with the older version of `jQuery` if you are able to `polyfill` in `promises/defer` within your code? – Woodrow May 03 '19 at 18:13
  • Nope. I just went ahead with a simple confirmation box – gene May 03 '19 at 18:17