Create new account I forgot my password    

Read Configuration Settings of Web.config using Javascript
Rating: 13 user(s) have rated this article Average rating: 3.5
Posted by: Suprotim Agarwal, on 3/25/2008, in category "ASP.NET 2.0 & 3.5"
Views: this article has been read 33334 times
Abstract: Web.config is a XML file containing the configuration settings of our application. Amongst its other uses, the file is extremely handy when it comes to changing configuration settings of an application, even when the application is live. You can define your own specific application settings, such as a database connection string using the tag and read and write values to the file programmatically. In this article, we see how to read the configuration settings in the web.config using ‘JavaScript’.

Read Configuration Settings of Web.config using Javascript
 
Web.config is a XML file containing the configuration settings of our application. Amongst its other uses, the file is extremely handy when it comes to changing configuration settings of an application, even when the application is live. You can define your own specific application settings, such as a database connection string using the <appSettings> tag and read and write values to the file programmatically. In this article, we see how to read the configuration settings in the web.config using ‘JavaScript’.
In one of the previous articles, we saw how we can use the API’s in the System.Configuration and System.Web.Configuration namespaces to read and write configuration settings in the web.config. In this short article, we will see how we can retrieve the values from the  <appSettings> and <connectionStrings> section in the web.config using JavaScript. So let us get started.
Step 1: Create a new ASP.NET website. Add a button control to the Default.aspx.
Step 2: Right click the project > Add New Item > Web Configuration File
Add the following sample entries in the file between the <configuration> tag as shown below:
<configuration>
      <appSettings>
            <addkey="var1"value="SomeValue"/>
      </appSettings>
      <connectionStrings>
            <addname="MyConnString"connectionString="Data Source=(local);Initial Catalog=Northwind;Integrated Security=True;"/>
      </connectionStrings>
...
<configuration>
 
Step 3: We will now read these entries using JavaScript. To do so, add the following script in the <head> tag of your Default.aspx page as shown below:
<head runat="server">
    <title>Read Config Entries Using Javascript</title>
    <script type="text/javascript">
    function ReadConfigSettings()
    {
        var conn = '<%=ConfigurationManager.ConnectionStrings["MyConnString"].ConnectionString %>'
        alert(conn);
        var v1 = '<%=ConfigurationManager.AppSettings["var1"].ToString() %>'
        alert(v1);
    }
    </script>
</head>
Step 4: Call this function on a button click and display the values of the configuration settings
   <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="ReadConfigSettings()" /></div>
 
That’s it. Run the application and click the button. The values of the configuration settings in the web.config will be displayed in the alert boxes.
I would like to thank JRICE from the ASP.NET forums for this tip.
I hope you liked this short article and I thank you for viewing it. If you liked the article,  Subscribe to my RSS Feed
 
 









Page copy protected against web site content infringement by Copyscape


How would you rate this article?

User Feedback
Comment posted by ravisankar on Thursday, April 03, 2008 8:27 AM
hi this is ravisankar. how to create the master page & sitemap plz explain that
Comment posted by ravisankar on Thursday, April 03, 2008 8:38 AM
hi this is ravisankar. how to create the master page & sitemap plz explain that
Comment posted by pavithra on Monday, June 16, 2008 10:51 PM
Hi,
If your .net application has 100 .aspx pages, from this
the  user should access only 20 .aspx pages then what changes will you make in your web.config file.
Comment posted by Shar on Wednesday, December 03, 2008 1:28 PM
Hi,

I am looking at some code where Javascript is used to validate the controls on the page and the error messages are retrieved from the AppSetting section of the web.config as in your blog.  Is this good practise? since javascript is client side and the error messages are in the web.config, is it hitting the server each time it retrieves the error string?  For localization, iss there a way to access the resource.resx instead via javascript instead?

Please let me know.

Thanks,

Shar
Comment posted by NS on Thursday, March 19, 2009 10:21 PM
Useless, even a beginner know this. i thought that it should be something tricky  
Comment posted by Raj on Monday, June 15, 2009 11:04 PM
Good example, but bad example.  Displaying and showing the web.config connection string?  I think it would have been better to show a general key.
Comment posted by Amit Habu on Thursday, July 23, 2009 7:32 AM
PLease can any one help me ,that how can i exceute my sql stmt via the given above connection
and achieve my records and dispaly it in browser
Comment posted by Deepak on Friday, November 27, 2009 5:09 AM
this code not working....
Comment posted by Nader H on Friday, January 15, 2010 2:00 PM
This code doesn't work. It doesn't shows values but actual text
Comment posted by petr on Wednesday, September 01, 2010 12:42 AM
this code not working....
Comment posted by Suprotim Agarwal on Thursday, September 02, 2010 12:51 AM
Code works just fine. What is the error you guys are getting?

Post your comment
Name:  
E-mail: (Will not be displayed)
Comment:
Insert Cancel

NEWSLETTER