sanidhya
sanidhya

Reputation: 1

passing javascript variable in app.settings (client side variable to server side)

I want to read the config file in javascript that is the google key value. here what i am trying to do is passing a javascript variable"keyvalue" that is the keyname in config file but it does not work. i just want it so that the js function automatically picks up the google key according to site URL whether it may be test or dev or qa.abc.com can anyone help me what should i do to read the value form config file.

  <script type="text/javascript">

function ReadConfigSettings()
{ 

var url="test.abc.com"; //window.location.href

var patharray= new Array();

patharray =url.split('.');

var first = patharray[0];

 var keyvalue="GoogleKey_"+ first;

 var key='<%=ConfigurationManager.AppSettings[keyvalue]%>';

 alert(key);
}

 </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="ReadConfigSettings()" />

    </div>
    </form>
</body>

my config file

<appSettings>
    <add key="GoogleKey_dev" value="ABQIAAAAJ4psDZ8kCtN062-LDcwiXhQ85a215fgrIsfghh547457h1ETJRYlwaBGrrytgytg56g7Mx4QFQ"/>
    <add key="GoogleKey_qa" value="ABQIAAAACoUjxmFCsPtytryhtyty547547Ryt5gVV28BYSHIaU0BRwPyLrf_gf546Jd_5qxcNZ-_b7WZw"/>
    <add key="GoogleKey_test" value="AIzaSyvbghgfyh54654650x2SlGb33KrTtIBc"/>

Upvotes: 0

Views: 1599

Answers (2)

Nikhil D
Nikhil D

Reputation: 2509

Web.Config

</configuration>
    <appSettings>
        <add key="Setting" value="Value"/>
    <appSettings>
</configuration>

In Aspx page take

<asp:HiddenField runat='server' id='hidkey' />

In javascript

 document.getElementById('<%=hidkey.ClientID %>').value = '<%=System.Configuration.ConfigurationManager.AppSettings["Setting"] %>';

Now access in C#

  string key=hidkey.value;//Here u get web.config [Setting] value

Upvotes: 1

Rab
Rab

Reputation: 35572

You Can't Mix server side with client side, like this. you need to use AJAX for this kind of functionality

Upvotes: 0

Related Questions