0

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>");
2
  • Calling alert is calling a function. What are you trying to do exactly and what issue are you having? Commented Jun 18, 2015 at 19:57
  • @RickS When I write Page.ClientScript.RegisterStartupScript(GetType(), "", "foo()", true); , it shows "Page.ClientScript.RegisterStartupScript(GetType(),"","foo()",true);" Commented Jun 18, 2015 at 20:08

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

8 Comments

It shows "Object reference not set to an instance of an object".
it works if there is no argument in the foo function, but if I have a date parameter passed, then how can I pass it with the code Page.ClientScript.RegisterStartupScript(GetType(),"","foo()",true). It works if there is no argument.
can I see how you are passing the arguments.
Page.ClientScript.RegisterStartupScript(GetType(), "a", "foo("aa","bb")", true); Foo has 2 arguments in the function.
the problem is not one or two variables, it is how they are being used. foo(' " + hello + " ', ' " + wor + " ');" without the single quotes, it thinks that hello and wor are javascript variables and looks for those variables in the <script> tags. Since they are declared and initialized in C# we need to pass them as strings to the javascript function.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.