1

I have an ASP button that executes some JavaScript "Generate()" and then the code behind "Button2_Click".

<asp:Button OnClientClick="Generate();" OnClick="Button2_Click" Text="New Baseline >>>" class="btn btn-primary" runat="server" Style="float: right;"/>

If "count" is greater than 0, I want to prevent the code behind from executing

function Generate() {
  var count = 0
  var ids = jQuery("#<%= JQGrid1.ClientID %>").jqGrid('getDataIDs');

  for (var i = 0; i < ids.length; i++) {
    var rowId = ids[i];
    var rowData = jQuery("#<%= JQGrid1.ClientID %>").jqGrid('getRowData', rowId);

    //alert(rowData.Action);

    if (rowData.Action == null || rowData.Action == "") {
      count += 1
    }
  }

  if (count > 0) {
    alert('You must complete all of the Actions fields first!');
    return false;
  } else {
    //All Action fields complete, continue to code behind...
  }
};

I have tried return false, but that does not stop the code behind.

2
  • What do you mean by "Stop code behind from executing"? You reference a function called "Button2_Click" but haven't included that code in the question. Commented Jan 16, 2020 at 20:22
  • If the JavaScript variable "count" is greater than 0, I don't want the code behind (Button2_Click) to execute. If the variable is 0 then I do want the Button2_Click function to execute. Commented Jan 16, 2020 at 20:24

2 Answers 2

1

You have to return the generate value to the Button.

<asp:Button OnClientClick="return Generate()"

<script>
    var test = 2;

    function Generate() {
        if (test === 1)
            return true;
        else
            return false;
    }
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

I don't know why this has been marked down, its the correct solution to my reported problem! Thanks VDWWD
0

Your problem lies in OnClientClick="Generate();" OnClick="Button2_Click".

You're assigning two inline click events here, so they'll both trigger independently.

You have to handle the Button2_Click function from inside Generate.

One way you might do this is to call Button2_Click in the else condition:

  if (count > 0) {
    alert('You must complete all of the Actions fields first!');
    return false;
  } else {
    //All Action fields complete, continue to code behind...
    Button2_Click();
  }

1 Comment

Button2_Click is an aspnet server side method. Not a javascript one...

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.