简体   繁体   English

从ASP.NET后台调用不是内联JS的Javascript函数

[英]Calling a Javascript function from ASP.NET code-behind that is not inline JS

I've found many examples of people calling inline JS functions that are in their HTML pages, but I'm wondering if it is possible to call JS functions that are in their own file. 我已经找到了许多人在其HTML页面中调用内联JS函数的示例,但是我想知道是否可以调用其自己文件中的JS函数。

For example if we have ~/Scripts/mainjs.js where we house most of our Javascript functions, is there any way to call functions in that file from the C# code-behind? 例如,如果我们拥有〜/ Scripts / mainjs.js,其中存放了大多数Javascript函数,是否有任何方法可以从C#代码背后调用该文件中的函数?

Edit: Also preferably passing parameters to the JS function 编辑:还最好将参数传递给JS函数

Try Like this 像这样尝试

string jsFunc = string.Format("myfunction('{0}');", parameterValue);
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "myJsFn", jsFunc, true);

Write this in your aspx.cs file Here myfunction is the Function name and parameterValue are the argument. 将其写入aspx.cs文件中。myfunction是Function名称,parameterValue是参数。

The below way may help you if you are using ASP.Net application: 如果您使用的是ASP.Net应用程序,则以下方法可以为您提供帮助:

  1. include the script file in aspx page 在aspx页面中包含脚本文件
  2. create a javascript function on the same aspx page which will call the function from js file 在同一个aspx页面上创建一个javascript函数,该函数将从js文件中调用该函数
  3. Use the newly created function in your c# code behind file 在文件的C#代码中使用新创建的函数

I know it is lengthy but may resolve your problem. 我知道这很长,但可以解决您的问题。

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

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