3

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' 
3
  • That should work (although is not the most recomended way to handle this). What does the alert show? an empty value? Commented Jun 26, 2009 at 13:36
  • Its displaying error Error 'System.Configuration.ConfigurationManager.AppSettings' is a 'property' but is used like a 'method' Commented Jun 26, 2009 at 13:40
  • I deleted my answer Manoj because it isn't what you need. Commented Jun 26, 2009 at 13:43

3 Answers 3

1

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()) %>;
Sign up to request clarification or add additional context in comments.

3 Comments

It is giving error Error The name 'json_encode' does not exist in the current context
any idea how I can add "addslashes" in javascript?
You need to do the addslashes bit in your server-side code not javascript.
1

Try this:

ConfigurationManager.AppSettings["vDirectory"].ToString()

Please note that square brackets are used instead of normal brackets.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.