问题
is it possible to invoke server variables in the external .js file ?
[Edit] OK, I resolved the problem, thank for Your help ! :)
回答1:
No, internal JS you can
<script>
var i = <%= ServerSideVar %>
</script>
回答2:
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).
回答3:
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));
来源:https://stackoverflow.com/questions/2412492/server-variables-and-the-js-file-is-it-possible