从 C# 调用 JavaScript 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18931936/
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
Call JavaScript function from C#
提问by Blossom
Javascript.js
Javascript.js
function functionname1(arg1, arg2){content}
C# file
C#文件
public string functionname(arg)
{
if (condition)
{
functionname1(arg1,arg2); // How do I call the JavaScript function from C#?
}
}
Please refer the above code and suggest me the idea to call a JavaScript function from C#.
请参考上面的代码并建议我从 C# 调用 JavaScript 函数的想法。
回答by Serjik
.aspx file in header section
标题部分中的 .aspx 文件
<head>
<script type="text/javascript">
<%=YourScript %>
function functionname1(arg1,arg2){content}
</script>
</head>
.cs file
.cs 文件
public string YourScript = "";
public string functionname(arg)
{
if (condition)
{
YourScript = "functionname1(arg1,arg2);";
}
}
回答by Bhavin Chauhan
If you want to call JavaScript function in C#, this will help you:
如果你想在 C# 中调用 JavaScript 函数,这将帮助你:
public string functionname(arg)
{
if (condition)
{
Page page = HttpContext.Current.CurrentHandler as Page;
page.ClientScript.RegisterStartupScript(
typeof(Page),
"Test",
"<script type='text/javascript'>functionname1(" + arg1 + ",'" + arg2 + "');</script>");
}
}
回答by SANDEEP
This may be helpful to you:
这可能对您有帮助:
<script type="text/javascript">
function Showalert() {
alert('Profile not parsed!!');
window.parent.parent.parent.location.reload();
}
function ImportingDone() {
alert('Importing done successfull.!');
window.parent.parent.parent.location.reload();
}
</script>
if (SelectedRowCount == 0)
{
ScriptManager.RegisterStartupScript(this, GetType(), "displayalertmessage", "Showalert();", true);
}
else
{
ScriptManager.RegisterStartupScript(this, GetType(), "importingdone", "ImportingDone();", true);
}
回答by SANDEEP
You can call javascript functions from c# using Jering.Javascript.NodeJS, an open-source library by my organization:
您可以使用Jering.Javascript.NodeJS(我的组织的开源库)从 c# 调用 javascript 函数:
string javascriptModule = @"
module.exports = (callback, x, y) => { // Module must export a function that takes a callback as its first parameter
var result = x + y; // Your javascript logic
callback(null /* If an error occurred, provide an error object or message */, result); // Call the callback when you're done.
}";
// Invoke javascript
int result = await StaticNodeJSService.InvokeFromStringAsync<int>(javascriptModule, args: new object[] { 3, 5 });
// result == 8
Assert.Equal(8, result);
The library supports invoking directly from .js files as well. Say you have file C:/My/Directory/exampleModule.js
containing:
该库也支持直接从 .js 文件调用。假设您有C:/My/Directory/exampleModule.js
包含以下内容的文件:
module.exports = (callback, message) => callback(null, message);
You can invoke the exported function:
您可以调用导出的函数:
string result = await StaticNodeJSService.InvokeFromFileAsync<string>("C:/My/Directory/exampleModule.js", args: new[] { "test" });
// result == "test"
Assert.Equal("test", result);