EverTheLearner
EverTheLearner

Reputation: 7200

Calling global variables in the AppSettings from a Javascript function

In my javascript function I am trying to call my global variable that is currently defined in the webconfig file of my Asp.net project. What is the syntax for this? I've looked around and nothing seems to work so far. I've tried the following but I am not getting anything from it:

web.config:

<appSettings>
  <add key="var1" value="123456"/>
</appSettings>

default.aspx:

<script type="text/javascript">
  function doSomething() {"<%= System.Configuration.ConfigurationManager.AppSettings['var1'].ToString(); %>"
};
</script>

Upvotes: 2

Views: 5189

Answers (1)

Guffa
Guffa

Reputation: 700562

What do you expect to get from it? Even if you manage to get the value, the code doesn't do anything at all with it. It just declares a string literal and throws it away...

You can put the value in a Javascript variable like this:

<script type="text/javascript">
var var1 = '<%=System.Configuration.ConfigurationManager.AppSettings["var1"].ToString();%>';
</script>

Now you can use that variable in your Javascript code.

Upvotes: 2

Related Questions