views:

212

answers:

3

Hi, is it possible to invoke server variables in the external .js file ?

[Edit] OK, I resolved the problem, thank for Your help ! :)

+2  A: 

No, internal JS you can

<script>
var i = <%= ServerSideVar %>
</script>
Dustin Laine
+1  A: 

When you say, "invoke a server variable" do you mean you want to change the value of a variable or did you mean to say, "invoke a function on the server?"

This can be done using an ajax call back to the server (assuming you are trying to avoid a postback).

Jason
In the server variable I have the function name that I need to invoke
Tony
Yes, in the .cs file I wrote public string CallbackMethod; CallbackMethod = Page.ClientScript.GetCallbackEventReference(this, "message", "Dodanie", "context", true);and then the function name that is being stored in CallbackMethod is needed to be invoked
Tony
A: 

Use Page.ClientScript.RegisterStartupScript() or Page.ClientScript.RegisterClientScriptBlock() to register the value of your server variables to JavaScript variables and then you can call them from the external .js file

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "variables", String.Format("var var1 = {1}; var var2 = {2};", someVariable1, someVariable2));
Ilya Volodin