Solved

Adding Javascript "if-else" to this onClick

Posted on 2008-06-17
7
1,495 Views
Last Modified: 2008-06-18
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...
0
Comment
Question by:Camillia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 82

Assisted Solution

by:hielo
hielo earned 100 total points
ID: 21806680
You can only use return within a function. This is incorrect:
if return

try:
<asp:Button ID="btnLookupByPostalCode" runat="server" OnClientClick="if( test() ){btnLookupByPostalCode_Click();}"  />
0
 
LVL 7

Author Comment

by:Camillia
ID: 21806797
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?
0
 
LVL 11

Expert Comment

by:spoxox
ID: 21807065
This javascript will work:

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


<button onClick="if (test()) {btnLookupByPostalCode_Click();}">Click me</button>
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Author Comment

by:Camillia
ID: 21807091
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.
0
 
LVL 11

Accepted Solution

by:
spoxox earned 400 total points
ID: 21807289
0
 
LVL 7

Author Comment

by:Camillia
ID: 21807363
thanks, let me see.
0
 
LVL 7

Author Comment

by:Camillia
ID: 21812677
This should do it but I cant get past one issue. I will close this question and open another one. Thanks,
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question