hello temp11
hello temp11

Reputation: 141

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>");

Upvotes: 0

Views: 3324

Answers (1)

laskdjf
laskdjf

Reputation: 1183

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.

Upvotes: 3

Related Questions