admin管理员组

文章数量:1320661

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 pilation 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?)

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 pilation 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?)
Share Improve this question asked Mar 3, 2013 at 9:00 IT_infoIT_info 7274 gold badges16 silver badges36 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 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. :)

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 mand back to the browser. In this case, you won't want to use OnClientClick at all.

本文标签: aspnetCall javascript and c method with one clickStack Overflow