Access C# variable in javascript file .js

后端 未结 3 1624
我寻月下人不归
我寻月下人不归 2021-01-19 05:08

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

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

Or do I hav

3条回答
  •  无人共我
    2021-01-19 05:28

    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.

    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.

    For example

    
    

    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.

    Some relative: How to get asp.net client id at external javascript file

提交回复
热议问题