简体   繁体   中英

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.

Js File

Function foo(date){
//code
}

In a cs file, I want to call the foo function. 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

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.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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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