Solved

Calling a Java script method from Asp.net, plus why is false returned?

Posted on 2009-07-01
4
230 Views
Last Modified: 2012-05-07

Hi, my Java Script is a little weak so bear with me.
My situation is that I have an Asp.net button. When it is pressed, it returns "false"
and my Ajax code works great. I need to enhance the code so that not only does it return
false, but it also calls another Java script method.

I tried returning false from the new Java script method, but it is not working for me. Can anyone tell me
what I need to do in order to have my New method called and then also to return false?

Here is my current code:
<asp:Button ID="btn_Animate" runat="server" Text="Animate" OnClientClick= "return false;" />


I need to enhance it so that it calls this method and also returns false. Note that I tried using the following
method but it did not work.


<script type="text/javascript">
function NewMethod(coord1,coord2) {

    coord1 = coord1 + coord2;
    return false;
}
</script>

Also dear experts I don't understand why false is returned in the first place? maybe you could help me understand
that as well. I will increase the points to accomidate the extra help in explaing why false need to be returned.
0
Comment
Question by:brgdotnet
  • 2
4 Comments
 
LVL 20

Accepted Solution

by:
ddayx10 earned 160 total points
ID: 24759301
Two options:

<asp:Button ID="btn_Animate" runat="server" Text="Animate" OnClientClick= "return NewMethod('parm1','param2');" />
<script type="text/javascript">
function NewMethod(coord1,coord2) {

    coord1 = coord1 + coord2;
    return false;
}
</script>


OR

<asp:Button ID="btn_Animate" runat="server" Text="Animate" OnClientClick= "NewMethod('parm1','param2'); return false;" />
<script type="text/javascript">
function NewMethod(coord1,coord2) {

    coord1 = coord1 + coord2;
}
</script>
0
 
LVL 5

Assisted Solution

by:mallcore
mallcore earned 40 total points
ID: 24759338
How about we do it something like this


<asp:Button ID="btn_Animate" runat="server" Text="Animate" OnClientClick= "return NewMethod();" />
<script type="text/javascript">
function NewMethod(coord1,coord2) {
 
    coord1 = coord1 + coord2;
    return coord1;
}
</script>

Open in new window

0
 
LVL 20

Expert Comment

by:ddayx10
ID: 24759378
Ah I forgot to answer the "Why is it false?" part.

Well if you return true from an action, let's say a button click, then the normal operation of that action will continue. In the case of a submit button it will post back the page. In the case of a link it would take you to another page.

If you return false from an action then the initial action is canceled. Button no longer causes the page to post back, just the JS function runs...etc.
0
 
LVL 2

Author Comment

by:brgdotnet
ID: 24766739
Thanks guys, you are awesome! It was the "return" part of the code that I was missing on the onButtonClick. So it is working now just great.
ddayx10 was the first to answer so I will give him the most point.

I need to take the time to figure out how to help others on this forum, as I would like to respond to other peoples questions as they need help. Have a great holiday!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

831 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