Access to C # variable in javasciprt.js file

Is it possible to create a javascript file with some C # variables? Something like that:

var foo =  <%= CODE_VALUE.foo %>;

Or do I need to create them in usercontrol.ascx, which I include in the section?

thank

+3
source share
3 answers

You cannot have this code inside a .js file because it is not compiled by asp.net and therefore does not translate the code CODE_VALUE.foointo code.

what you can do is put these variables right before loading this .js file that you won for use, and set them on any aspx or user control.

for instance

<script>
 var foo = <%=CODE_VALUE.foo%>;
<script>
<script type="text/javascript" src="fileToUseTheFoo.js">   
</script>

, javascript, javascript javascript.

: asp.net javascript

+10

, .js. / , .

0

Javascript - , ASP.NET.

So, if you really want to insert this value, you can create an empty .aspx page and include all javascript in it, and then include the .aspx page in script src.

But I would rather have a web service instead using a web method that you would call from javascript every time you needed to get a value.

0
source

All Articles