DotNet Mirror
  DNM facebook   DNM Google+   DNM Twitter   

Reading configuration settings (connection string and app settings) values of Web.config in JavaScript

By Ashok Nalam on 30 Dec 2012 | Category: JavaScript & Jquery | Tagged: web-config javascript ASP.NET 
In this snippet, we will see how to read values which are defined in appsettings and connectionStrings sections of web.config in JavaScript using ASP.NET.
  Discuss Add Comment   |  
Add rating Rate this resource  [Rated 2.00/5 by 1 people] 

Web.config file has configuration settings for ASP.NET web application. We will store application level settings and connection string value in it. In this snippet, we will see how to read values which are defined in appsettings and connectionStrings sections of web.config in JavaScript using ASP.NET.

In the following code, we will add values for appSettings and connectionStrings sections of web.coinfig file then from JavaScript we will try to read the values.

Web.config

<configuration>
  <appSettings>
    <add key="appKeyPI" value="3.15"/>
  </appSettings>
  <connectionStrings>
    <add name="conStr" connectionString="Here is my connection string"/>
  </connectionStrings>
</configuration>

ASPX Page

<head>
    <script type="text/javascript">
        var appKeyVal = '<%=ConfigurationSettings.AppSettings["appKeyPI"].ToString()%>';
        var conStrVal = '<%=ConfigurationManager.ConnectionStrings["conStr"].ConnectionString %>';
        alert("AppSettings value is: " + appKeyVal);
        alert("Connection string value is: " + conStrVal);
    </script>
</head>

output:

we will get two alter's with text as "AppSettings value is: 3.15" and "Connection string value is: Here is my connection string"

Basically in JavaScript, we tried to access the configuration settings values with server side code using <%= %>.

  Discuss Add Comment    
Add rating Rate this resource  [Rated 2.00/5 by 1 people] 
About the Contributor
Member Since : 10 Dec 2012
Member Points (Level) : 7003  (Expert)
Location : INDIA
Home Page : http://dotnetmirror.com
About : I am admin of this site.
Rate this resource
 
Add your Comment
Name Email WebSite
Captcha Refresh


Comments (1)
 
1. By ramendra on 09 Jan 2013

It does't work it gives error Identifier expected