0

I am trying to read key value from web.config using javascript in my .aspx page. This page is included in a master page, the script block is added in the ContentPlaceHolder tag in my aspx page as below; This question was asked before here how to read values from web.config in javascript in aspx page but there was no clear solution. Someone suggested returning to server side but I am trying to avoid returning to server in order to accelerate user's work on the page ... So below is my code:

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <script type="text/javascript">

    function CheckCountry() {
        var country = '<%= System.Configuration.ConfigurationManager.AppSettings["LoginCountry"].ToString() %>';
        alert(country);
    }

    window.onload = CheckCountry; 

    </script>
       ....
</asp:Content>

But whenever I try to run my project I get this error in the List Error box in visual Studio 2010. I have tried also this '<%=ConfigurationManager.AppSettings["LoginCountry"].ToString() %>' but didn't work, same error was thrown ... What could be the problem? How can I solve this and get the value of country from web.config in my aspx page?

5
  • Well, it worked when I tried it... What does your web.config <appSettings> node look like? Commented Mar 14, 2017 at 13:40
  • @VDWWD that's it <appSettings> <add key="LoginCountry" value="LL"/> </appSettings> Commented Mar 14, 2017 at 13:41
  • Looks correct. What happens if you just ignore the error. Or remove the ' from the javascript. Sometimes the editor doesn't understand javascript combined with inline code. Commented Mar 14, 2017 at 13:46
  • @VDWWD If I ignore it it will be thrown when I open this page; how do I write it if I remove the ' ? Commented Mar 14, 2017 at 13:51
  • could it be because <appSettings> is inside a <location allowOverride="true" inheritInChildApplications="true"> tag ? Like here: <location allowOverride="true" inheritInChildApplications="true"><appSettings> <add key="LoginCountry" value="LL"/> </appSettings></location> Commented Mar 14, 2017 at 13:55

1 Answer 1

1

It would seem the Visual Studo 2010 editor gets confused about the combination of ' and the inline code <%= ... %>. Because in Studio 2015 the following line does work:

var country = '<%= System.Configuration.ConfigurationManager.AppSettings["LoginCountry"].ToString() %>';

What you could try is to create the entire line with javascript in code, maybe then the editor is not confused.

<%= "var country2 = '" + System.Configuration.ConfigurationManager.AppSettings["LoginCountry"].ToString() + "';" %>

Or in code behind:

public string javaScript;
protected void Page_Load(object sender, EventArgs e)
{
    javaScript = "var country = '" + ConfigurationManager.AppSettings["LoginCountry"].ToString() + "';";
}

and then on the aspx page

function CheckCountry() {
    <%= javaScript %>
    .... etc
}
Sign up to request clarification or add additional context in comments.

5 Comments

Where in code? Did you meant in code behind? Can you give me an example please, because I paste it in my javascript function instead of var country = '<%= System.Configuration.ConfigurationManager.AppSettings["LoginCountry"].ToString() %>'; but it has thrown an error: Operator '+' is not defined for types 'String' and 'System.Collections.Specialized.NameValueCollection'.
If it was in code behind, does my aspx page could see it in Window.onload event?
Both the first examples are inline on the aspx page (reconized by the <%= %>
Thank you for your reply, it ended up by declaring javaScriptVal as public string (as you suggested) then filling it directly by the configuration value in this way: javaScriptval = System.Configuration.ConfigurationManager.AppSettings("LoginCountry").ToString , than I made this in my javascript function : var t = '<%= javaScriptval %>'; alert(t); and it alerted LL.I have tried this solution on button click and it is working fine, and on alert it does not return to server side to get the value as it is public.
I discovered my mistake: [] are for c# but () are for .NET, now it works like a shine

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.