1

I have a method in javascript and a method in c#, now with a click on a button I would like to call them both.

First I would like to call the c# method named as 'Find_Direction' since it gets to inputs onclick and then I will call after the javascript method named as calcRoute.

I was trying this, but without any luck:

<asp:Button id="Button1" UseSubmitBehavior="False" value="GetDirections" onclick="calcRoute" OnClientClick="Find_Direction" runat="server"/>

The following error popped up :

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1061: 'ASP.findroute_aspx' does not contain a definition for 'calcRoute' and no extension method 'calcRoute' accepting a first argument of type 'ASP.findroute_aspx' could be found (are you missing a using directive or an assembly reference?)

2 Answers 2

3

Your are specifying javascript function name in onClick event. So put here C# function name

C# code:

 protected void  Find_Direction (object sender, EventArgs e)
 {
   // do your work

   // Register your javascript function

    Page.ClientScript.RegisterStartupScript(this.GetType(), "script", "  <script>calcRoute();</script>");
 }

your markup :

<asp:Button id="Button1" value="GetDirections" onclick="Find_Direction" runat="server"/>

Case 2 : Call javascript function before server side function

Javascript function

 function calcRoute() {

        alert('test')
        return true;
    }

your markup

 <asp:Button id="Button1" value="GetDirections" onclick="Find_Direction " OnClientClick="return Find_Direction()" runat="server"/>

By this you can call javascript function then server side but make sure you are returning true from javascript function for calling your server side function.

I Hope, now you can solve your problem. :)

Sign up to request clarification or add additional context in comments.

9 Comments

This way will the find_direction method be executed first? @Sachin
No. I thought that you want find_Direction to be executed after calcRoute. But no problem if you want find_Direction to be execute first than just call it on client click like OnClientClick="return Find_Direction".
I am still struggling with it. Since it seems that it is not going in the javscript method. @Sachin
Onclick it is going in the Find_Direction method, but than it is not going through the javascript one
@IT_info I have edited my answer, using case 2, you can call Find_Direction first and then calcRoute after that. I removed UseSubmitBehavior="False" from the markup.have a look.
|
2

You need an event handler in your code-behind (the aspx.cs or aspx.vb file) called calcRoute.

If you want the JS to execute first, you should be able to do it the way you're doing it, with OnClientClick which points to a JS function and OnClick which points to a C#/VB method (event handler).

If you want to be sure that JS gets called after your C# code, you should handle the click on the backend, and then send the JS back to the browser by registering a startup script. This will cause a full post-back, unless you're using an update panel. In your event handler, you can register a startup script with Page.RegisterStartupScript to send a javascript command back to the browser. In this case, you won't want to use OnClientClick at all.

2 Comments

But which will be processed first? the calcRoute or the Find_Direction method. I want to be processed first the Find_Direction. @Adam Plocher
Oh sorry, I was thinking you wanted it the other way. OnClientClick should be executed before the post-back/event handler.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.