Solved

Adding Javascript "if-else" to this onClick

Posted on 2008-06-17
7
1,492 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
  • 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBScript on Html 15 47
Google Analytics setup 5 50
Explain this javascript function to me 2 24
Html value of radio 14 27
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

790 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