How to read WebConfig Connection Strings & App Settings in Java Script? - Asp.Net
Read Configuration Settings of Web.config using Javascript
Web.config is the main settings and configuration file for an ASP.NET web application. We store application level settings and connection string value in it. In this post I will explain how to read those values from web.config easily. Find the source code below:-
Web.Config
<appSettings>
<add
key="Blog" value="www.etechpulse.com"/>
</appSettings>
<connectionStrings>
<add
name="NorthwindConnectionString" connectionString="Data
Source=serverName;Initial Catalog=Northwind;Persist Security Info=True;User
ID=userName;Password=password" providerName="System.Data.SqlClient"
/>
</connectionStrings>
Default.aspx
<%@ Page Language="C#"
AutoEventWireup="true"
CodeFile="Default.aspx.cs"
Inherits="_Default"
%>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>How to
read Webconfig Connection Strings & App Settings in Java Script - Asp.Net </title>
<script type="text/javascript">
// Read appSettings key value from web.config
function
ReadWebConfigappSettings()
{
var
connectionString = '<%=ConfigurationManager.AppSettings["blog"]
%>'
document.getElementById("lblReadWebConfigappSettings").innerHTML
= connectionString;
}
// Read connectionStrings from web.config
function
ReadWebConfigconnectionStrings()
{
var
connectionString = '<%=ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString
%>'
document.getElementById("lblReadWebConfigconnectionStrings").innerHTML
= connectionString;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="btnAS" type="button" value="Read Web.ConfigAppSettings" onclick="ReadWebConfigappSettings();"
/>
<input id="btnCS" type="button" value="Read Web.ConfigConnectionStrings" onclick="ReadWebConfigconnectionStrings();"
/>
<br />
<br />
<asp:Label ID="lblReadWebConfigappSettings" runat="server"
Text=""></asp:Label>
<br />
<br />
<asp:Label ID="lblReadWebConfigconnectionStrings" runat="server"
Text=""></asp:Label>
</div>
</form>
</body>
</html>
Download the source code click here ..
How to read WebConfig Connection Strings & App Settings in Java Script? - Asp.Net
Reviewed by Ravi Kumar
on
11:38 AM
Rating:

No comments: