Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1553
  • Last Modified:

Adding Javascript "if-else" to this onClick

I have a js code like this:

 function test()
   {  alert("hello") ;  return true;}

***I have a button like this:
<asp:Button ID="btnLookupByPostalCode" runat="server" OnClick="btnLookupByPostalCode_Click"  />

  -- I want to add something like this :  onClick= "if return(test() == true) { btnLookupByPostalCode_Click; } "

can I do that?? I get errors...
  • 4
  • 2
2 Solutions
You can only use return within a function. This is incorrect:
if return

<asp:Button ID="btnLookupByPostalCode" runat="server" OnClientClick="if( test() ){btnLookupByPostalCode_Click();}"  />
CamilliaAuthor Commented:
that btnLookupByPostalCode_Click is ASP.Net's onClick event for the button. It's not a JS function so I get an error that object is required.

I dont think I can call a ASP.Net onClick event from JS. no?
This javascript will work:

<script type="text/javascript">
function test() {
return true;
function btnLookupByPostalCode_Click() {

<button onClick="if (test()) {btnLookupByPostalCode_Click();}">Click me</button>
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

CamilliaAuthor Commented:
As I said : btnLookupByPostalCode_Click is NOT a javascript function. It's C#'s onClick event for the button. That code is NOT a JS code. It's C# in code behind.
CamilliaAuthor Commented:
thanks, let me see.
CamilliaAuthor Commented:
This should do it but I cant get past one issue. I will close this question and open another one. Thanks,
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now