Server variables and the .js file. Is it possible?

我的未来我决定 提交于 2019-12-25 04:49:20

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!