Solved

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

Posted on 2009-07-01
4
229 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now