views:

65

answers:

3

Hi to All,

I have a javascript as

<script type="text/JavaScript">
   function CalculateDistance (plat1, plon1, plat2, plon2) 
   {
       try
       {  
           :
           :
           var d = R * c;

           return d;
       }
       catch (error) 
       {
               alert(error);
       }
    }
</script>

I want to call it from my asp.net page. How to call this type of javascript on page load and button click with catching the return value. I want to catch the distance by proiding the 4 input parameter.

Please reply.........

Regards, Girish

A: 

Check this question : How can I call javascript method from my aspx.cs file?

Shoban
A: 

As far as I know you can't return value from javascript to server side, better to put this method on server side and use over there.

but there is alternate way you can do this by using hidden field, put your value in hidden field in javascript function and use in server side.

Muhammad Akhtar
+2  A: 

Try this for calling function on page load: <body onload="CalculateDistance(...);">

and for calling function on click event of a button try this:

 <asp:Button ID="btn" Text="Save" runat="server" OnClientClick="CalculateDistance(...); return false;" />

And to catch the return value you can use a hidden field control:

 <asp:HiddenField ID="hdn" runat="server" />

and assign the return value of the javascript function to this hiddenfield try the following

<asp:Button ID="btn" Text="Save" runat="server" OnClientClick="document.getElementById('<%= hdn.ClientID %>').value=CalculateDistance(...); return false;" />
Himadri