Reputation: 1153
I am using this tutorial to use a variable from web.config file into .NET . Now I want to use the exact same variable in JavaScript but according to my research the WebConfigurationManager
variable is not available in HTML , any clue on how to do that ?
thanks
EDIT :
I tried to implement the suggested code in that way (just to make sure that it gives me the output I want):
var myJsVar = '<%= ConfigurationSettings.AppSettings["MyConfigValue"] %>';
alert(myJsVar);
the value of myJsVar
come up as
<%= ConfigurationSettings.AppSettings["MyConfigValue"] %>
and when I do '<%= ConfigurationSettings.AppSettings["MyConfigValue"] %>'
and when I do alert(myJsVar.valueof());
instead I receive undefined
as output
I also tried to use WebConfigurationManager
instead of ConfigurationSettings
and it goes through the same logic ...
Upvotes: 0
Views: 2245
Reputation: 4854
You can render the value to the aspx page, as an assignment to a JavaScript variable:
var myJsVar = '<%= ConfigurationSettings.AppSettings["MyConfigValue"] %>';
So this way, myJsVar
will be initialized with MyConfigValue
. Hope this helps.
EDIT
If you are using Razor you should use this way
var myJsVar = '@System.Configuration.ConfigurationManager.AppSettings["MyConfigValue"]';
Upvotes: 3
Reputation: 37543
You just need to output the setting into the variable you want. Somewhere in your javascript you would put:
<script language="javascript">
var mySetting = '<%=ConfigurationManager.AppSettings["mySetting"] %>';
</script>
Here the <%=
is a Response.Write
command that will output the string result to the markup rendered. From this point on the variable mySetting
would be accessible to any components.
Upvotes: 3