简体   繁体   English

一键调用javascript和c#方法

[英]Call javascript and c# method with one click

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. 我在javascript中有一个方法,在c#中有一个方法,现在单击一个按钮,我想同时调用它们。

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 . 首先,我想调用名为'Find_Direction'的c#方法,因为它可以输入onclick,然后在名为calcRoute的javascript方法之后调用。

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

Your are specifying javascript function name in onClick event. 您正在onClick事件中指定javascript函数名称。 So put here C# function name 所以放在这里C#函数名称

C# code: C#代码:

 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 情况2:先在服务器端函数之前调用javascript函数

Javascript function Javascript功能

 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. 这样,您可以先调用javascript函数,然后再调用服务器端,但是请确保从javascript函数返回true来调用服务器端函数。

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 . 您需要在代码隐藏(aspx.cs或aspx.vb文件)中的事件处理程序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). 如果希望JS首先执行,则应该能够按照自己的方式进行操作,其中OnClientClick指向JS函数,OnClick指向C#/ VB方法(事件处理程序)。

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. 如果要确保在C#代码之后调用JS,则应处理对后端的单击,然后通过注册启动脚本将JS发送回浏览器。 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. 在事件处理程序中,可以使用Page.RegisterStartupScript注册启动脚本,以将javascript命令发送回浏览器。 In this case, you won't want to use OnClientClick at all. 在这种情况下,您根本不会使用OnClientClick。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM