tags:

views:

844

answers:

2

Hi,

I have web.config with the given value:

<appSettings>
     <add key="vDirectory" value="fr" />
     <add key="BookingSummaryPage" value="/pli/forms/BookingSummary.aspx" />
</appSettings>

Now I want to read the value of "vDirectory" through java script.

I am using below code:

<script language="javascript" type="text/javascript">

function test()
{
var t='<%=ConfigurationManager.AppSettings("vDirectory").ToString() %>'
alert(t);
}
</script>

<input type="button" value="Click Me" onclick="test();" />

The error generated is:

Error 'System.Configuration.ConfigurationManager.AppSettings' is a 'property' but is used like a 'method'
A: 

Edit: this doesn't answer your first issue, but still applies after you fix that. If vDirectory was something like "c:\new folder" you'd end up with a newline in t.

I'm not sure what language you're using but you want to run the string though addslashes() (or the equivalent in your language) before you print it out like that:

var t='<%=addslashes(ConfigurationManager.AppSettings("vDirectory").ToString()) %>';

Or even better, JSON encode it if there's a function for that:

// Note no quotes as json_encode will add them
var t=<%=json_encode(ConfigurationManager.AppSettings("vDirectory").ToString()) %>;
Greg
It is giving error Error The name 'json_encode' does not exist in the current context
MKS
any idea how I can add "addslashes" in javascript?
MKS
You need to do the addslashes bit in your server-side code not javascript.
Greg
A: 

If it's a property (variable), you can't call it, like its a method (function). So don't you need:

<%=ConfigurationManager.AppSettings.GetKey("vDirectory")%>

...?

http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings.aspx

Lee Kowalkowski