Solved

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

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

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.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

820 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