Solved

Adding Javascript "if-else" to this onClick

Posted on 2008-06-17
7
1,498 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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
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

Independent Software Vendors: 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!

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

734 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