Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 asp.net - how to read values from web.config in javascript in aspx page - Stack Overflow[^] 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 (Identifier expected ) in the List Error box in visual Studio 2010. What could be the problem? How can I solve this and get the value of country from web.config in my aspx page?

What I have tried:

I have tried also this
'<%=ConfigurationManager.AppSettings["LoginCountry"].ToString() %>'

but didn't work, same error was thrown ...
Posted
Updated 14-Mar-17 4:26am
Comments
F-ES Sitecore 14-Mar-17 10:09am    
Your code works fine for me.
H.AL 14-Mar-17 10:13am    
It seems it works on visual 2015 but not on visual 2010, any suggestions?

1 solution

Look like you using vb.net instead of c# -- brackets []

Here is the correct syntax for vb.net -- Parentheses ()

HTML
var country =  System.Configuration.ConfigurationManager.AppSettings("LoginCountry").ToString()


By the way, I did get the same error "Identifier expected" using the code with [] in vb.net project VS2010. Replacing the brackets with parentheses make the error go away. So, I'm assuming that your project is using VB.NET language.
 
Share this answer
 
v2
Comments
F-ES Sitecore 14-Mar-17 10:48am    
Nice catch :)
H.AL 15-Mar-17 1:44am    
Thank you very much, what a stupid mistake ...
Bryian Tan 15-Mar-17 1:46am    
You're very welcome. Mistake happen, that part of the learning curve.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900