简体   繁体   English

从C#代码在js文件中调用函数

[英]Calling a function in js file from C# code

I am having a cs file where I want to call the function of js file. 我有一个CS文件,我想在其中调用js文件的功能。

Js File Js文件

Function foo(date){
//code
}

In a cs file, I want to call the foo function. 在一个cs文件中,我想调用foo函数。 Is it possible ? 可能吗 ? If yes, then how ? 如果是,那怎么办?

If I want to insert just one alert then this is how I do it, but If I want to call a function, then I dont know the way. 如果我只想插入一个警报,那么这就是我的方法,但是如果我想调用一个函数,那么我不知道该怎么做。

To call just an inline javascript this works 仅调用内联JavaScript即可

Page.ClientScript.RegisterStartupScript(GetType(), "", "<script type='Text/JavaScript'> alert('Hello');</script>");

Try putting the function name rather than the whole script itself. 尝试放置函数名称而不是整个脚本本身。

Page.ClientScript.RegisterStartupScript(GetType(),"","foo()",true);

or with ScriptManager: 或使用ScriptManager:

ScriptManager.RegisterClientScriptBlock(this, this.GetType(),"","foo()",true);

Edit: 编辑:

Add the boolean value at the end to true , so it will automatically add script tags, then you dont need to. 将boolean值添加到true的末尾,这样它将自动添加脚本标签,然后您不需要这样做。

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

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