javascript 一键调用javascript和c#方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15184097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 23:47:23  来源:igfitidea点击:

Call javascript and c# method with one click

javascriptasp.netbutton

提问by IT_info

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.

首先,我想调用名为 as 的 c# 方法,'Find_Direction'因为它可以输入 onclick,然后我将调用名为 as 的 javascript 方法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?)

回答by Sachin

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

您正在 onClick 事件中指定 javascript 函数名称。所以把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. :)

我希望,现在你可以解决你的问题。:)

回答by Adam Plocher

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

如果你想确保 JS 在你的 C# 代码之后被调用,你应该处理后端的点击,然后通过注册一个启动脚本将 JS 发送回浏览器。这将导致完全回发,除非您使用更新面板。在您的事件处理程序中,您可以注册一个启动脚本Page.RegisterStartupScript以将 javascript 命令发送回浏览器。在这种情况下,您根本不想使用 OnClientClick。