0

I have this js function :

function neki() {
  if ( $("input[id*='txtIDCriteria']").val() == ''
    && $("input[id*='chkID']").is(':checked')
  ) {
    alert('Value is required.');
  }
}

And this asp code:

<table border="0" cellpadding="1" cellspacing="0"
  style="width: 100%" class="DataTable" id="check_all"
>
  <tr>
    <td>
      <asp:CheckBox CssClass="formdata100" ID="chkID" runat="server"> 
      </asp:CheckBox>
    </td>
    <td>
      <asp:Label CssClass="formdata100" ID="lblID" runat="server">
        Maintenance ID
      </asp:Label>
    </td>
    <td>
      <asp:TextBox CssClass="formdata100" ID="txtIDCriteria"
        runat="server" MaxLength="6"
        onkeyup="return validate_int(event,this);"
      >
      </asp:TextBox>
    </td>
  </tr>
  <tr>
    <td colspan="5">
      <asp:Button Text="search" ID="search" OnClick="search_Click"
        runat="server" style="float:right" onClientClick="neki()"
      />
    </td>
  </tr>

I want this OnClick="search_Click" to stop when this function neki() is executed ie when it is true.

How to solve it through JavaScript?

3
  • Javascript is single threaded. You can't just signal a function to stop. Commented Feb 2, 2022 at 19:03
  • Is it possible to solve in another way? Commented Feb 2, 2022 at 20:54
  • Yes, you can have the button code run, or not. If the on client event returns true, then server side button code will run. If the on client click JS routine returns false, then the server side button click and code behind does not run. See answer below. Commented Feb 2, 2022 at 21:17

1 Answer 1

1

Yes, you can do this. The way this works?

If the onclient click function returns true, then the server side code/event runs.

If the onclient click function returns false, then the server side code/event does NOT run. So you can do this:

   <asp:Button Text="search" ID="search" OnClick="search_Click"
    runat="server" style="float:right" onClientClick="return neki();"

And now your JS code can be this:

function neki() {
    if ( $("input[id*='txtIDCriteria']").val() == ''
        && $("input*[id*='chkID']").is(':checked'))
    {
    alert('Value is required.');
    return true;
    }
    return false;
}

While this will work for above, if you use a jQuery.UI dialog, the code does not wait, but there is a simple work around for even jQuery.UI dialog that respond to say a yes/no answer, and you can still conditional run the server side button code.

edit: opps - I have this backwards.

the code should be like this:

function neki() {
    if ( $("input[id*='txtIDCriteria']").val() == ''
        && $("input*[id*='chkID']").is(':checked'))
    {
    alert('Value is required.');
    return false;  <--- return false to NOT run button click
    }
    return true;   <--- return true - this will allow button click to run
}
Sign up to request clarification or add additional context in comments.

5 Comments

Unfortunately, it doesn't work.
yes, it should work - but in my example, I have the true and false values backwards. The routine has to return false for a fail, and true to run. I have edited the example code to reflect this:
Works, but I had to swap places true and false.
Thank you, a minute ago I figured out what I did and I see you answered before I refreshed the page. ;D
yes, see my edit - that's what happens when I post "air code", but yes, it should work. Just keep in mind for say a jQuery.UI dialog, the code does NOT wait, so you have to slight change the code in that case, but for alert(), or code that waits - you are ok .

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.