在javasciprt文件的.js访问C#变量 [英] access c# variable in javasciprt file .js
问题描述
是否有可能创建一些C#变量javascript文件?事情是这样的:
Is it possible to create a javascript file with some c# variables? Something like this:
var foo = <%= CODE_VALUE.foo %>;
还是我在usercontrol.ascx其中包括我的部分可以创建他们?
Or do I have to create them in a usercontrol.ascx which I include in the section?
感谢
推荐答案
您不能有.js文件这里面code,因为不能被asp.net编译,因此不翻译 code_VALUE.foo
为code。
You can not have this code inside the .js file because is not compile by asp.net and so is not translate the CODE_VALUE.foo
into code.
你可以做的是把这个变量只加载在此之前.js文件,你赢了使用的文件,并将它们在任何ASPX或用户控制。
what you can do is to place this variables just before load this .js file that you won to use, and set them on any aspx or user control.
例如
<script>
var foo = <%= CODE_VALUE.foo %>;
<script>
<script type="text/javascript" src="fileToUseTheFoo.js">
</script>
替代你可以创建读取JavaScript文件的处理程序,包括自定义的变量,并创建一个完整的JavaScript code,只要你喜欢,并把它作为JavaScript的之一。
alternative you can create a handler that read javascript files, include your custom variables and create a full javascript code as you like, and send it as javascript one.
一些相对的:<一href=\"http://stackoverflow.com/questions/3845900/how-to-get-asp-net-client-id-at-external-javascript-file/3851247#3851247\">How在外部JavaScript文件,以获得asp.net客户端ID
这篇关于在javasciprt文件的.js访问C#变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!