从Javascript函数调用AppSettings中的全局变量

时间:2022-12-28 16:43:26

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:

在我的javascript函数中,我试图调用我的Asp.net项目的webconfig文件中当前定义的全局变量。这是什么语法?我环顾四周,到目前为止似乎没有任何工作。我尝试了以下但我没有得到任何东西:

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 个解决方案

#1


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:

您可以将值放在Javascript变量中,如下所示:

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

Now you can use that variable in your Javascript code.

现在,您可以在Javascript代码中使用该变量。

#1


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:

您可以将值放在Javascript变量中,如下所示:

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

Now you can use that variable in your Javascript code.

现在,您可以在Javascript代码中使用该变量。