2

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>

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.