views:

389

answers:

1

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>
A: 

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.

Guffa
I shortened my code to better show the problem I was having. Thanks to you I can now use that information to work on my javascript
Roy