Click here to Skip to main content
15,887,471 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am reading the web.config key value to my javascript function.
I am getting below exception
The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
<script language="javascript" type="text/javascript">
   function test()
   {
      alert(1);
      var key1 = '<%=System.Configuration.ConfigurationManager.AppSettings["AESKey"].ToString() %>';
       alert(key1);

   }
   </script>


What I have tried:

Below is the complete page level source
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    
    <script language="javascript" type="text/javascript">
    function test()
    {
       alert(1);
       var key1 = '<%=System.Configuration.ConfigurationManager.AppSettings["AESKey"].ToString() %>';
        alert(key1);
        
    }
    </script>
    
</head>
<body >
 
    <form id="form1" runat="server">
    <table width="997" border="0" cellspacing="0" cellpadding="0" align="center">
        <tr>
            <td>
                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                        <td width="40%" class="NeST_LOGIN_IMG1">
                        </td>
                        <td width="3%" class="NeST_LOGIN_IMG2">
                        </td>
                        <td width="28%" class="NeST_LOGIN_IMG3">
                        </td>
                        <td width="29%" align="right" height="135">
                            <table width="50%" border="0" cellspacing="0" cellpadding="0" class="NeST_BANK_LOGO1">
                                <tr>
                                    <td width="11%" rowspan="3"><img src="images/sec/ddsedge_04.jpg" width="25" height="135" /></td>
                                    <td width="79%" style="background-image: url(images/sec/ddsedge_05.jpg)"><img src="images/ddsedge_07.jpg" width="120" /></td>
                                    <td width="10%" rowspan="3"><img src="images/sec/ddsedge_06.jpg" width="24" height="135" /></td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td>
                <table width="100%" border="0" cellspacing="0" cellpadding="0" id="tblLoginPart">
                    <tr>
                        <td width="40%" class="NeST_LOGIN_IMG9">
                        </td>
                        <td width="1%" class="NeST_LOGIN_IMG10">
                        </td>
                        <td width="59%" class="NeST_LOGIN_IMG11" valign="top">
                            <div class="NeST_BANK_LOGO2" > 
                            
                             <table width="50%" border="0" cellspacing="0" cellpadding="0" >
                                    <tr>
                                        <td width="11%" rowspan="3">
                                            <img src="images/sec/ddsedge_04.jpg" width="25" height="135" />
                                        </td>
                                        <td width="79%" style="background-image: url(images/sec/ddsedge_05.jpg); background-repeat:repeat-x ;">
                                           <img src="images/NBKddsedge_07.jpg" width="120px" />
                                        </td>
                                        <td width="10%" rowspan="3">
                                            <img src="images/sec/ddsedge_06.jpg" width="24" height="135" />
                                        </td>
                                    </tr>
                                </table>
                            
                            </div>
                            <table width="100%" border="0" cellspacing="0" cellpadding="0">
                                <tr>
                                    <td width="12%" height="122">
                                         
                                    </td>
                                    <td width="68%">
                                         
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                         
                                    </td>
                                    <td>
                                        <label class="NeST_LOGIN_LABEL">
                                            Username
                                        </label>
                                        <asp:RequiredFieldValidator ID="Requiredfieldvalidator1"
                                            runat="server" ControlToValidate="txtLoginID" ErrorMessage="[Username cannot be empty]"
                                            CssClass="VALIDATECLASS_LOGIN" EnableClientScript="False"></asp:RequiredFieldValidator>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                         
                                    </td>
                                    <td>
                                        <asp:TextBox runat="server" ID="txtLoginID" name="textfield" 
                                            CssClass="NeST_LOGIN_TEXT" AutoCompleteType="Disabled" />
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                         
                                    </td>
                                    <td>
                                        <label class="NeST_LOGIN_LABEL">
                                            Password
                                        </label>
                                       
                                        <asp:RequiredFieldValidator ID="Requiredfieldvalidator2" runat="server" ControlToValidate="txtPassword"
                                            ErrorMessage="[Password cannot be empty]" CssClass="VALIDATECLASS_LOGIN" EnableClientScript="False"></asp:RequiredFieldValidator>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                         
                                    </td>
                                    <td>
                                        <asp:TextBox runat="server" ID="txtPassword" TextMode="Password" name="textfield2"
                                            onkeypress="return RestrictSpace()" oncopy="return false" onpaste="return false"
                                            oncut="return false" CssClass="NeST_LOGIN_TEXT" 
                                            AutoCompleteType="Disabled" />
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                         
                                    </td>
                                    <td>
                                         
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                         
                                    </td>
                                    <td>
                                        <label>
                                        </label>
                                        <table width="100%" border="0" cellspacing="0" cellpadding="0">
                                            <tr>
                                                <td width="11%">
                                                     
                                                </td>
                                                <td width="9%">
                                                     
                                                </td>
                                                <td width="80%" align="center">
                                                    <asp:Button ID="btnLogin" runat="server" CssClass="buttonLogin" Text="Login" runat="server"
                                                      OnClientClick="javascript:test();"   OnClick="btnLogin_Click" TabIndex="3" Width="60px"></asp:Button>
                                                  
                                                </td>
                                            </tr>
                                        </table>
                                        <label>
                                        </label>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td  >
                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                        <td width="41%"  height="62" align="right" class="NeST_LOGIN_LOGO_SMALL" ></td>
                        <td width="59%" class="footertext"> © 2013 NeST IT. All rights reserved</td>
                    </tr>
                </table>
            </td> 
        </tr>
    <tr>
        <td>
             
        </td>
    </tr>
    </table>

    <script>

        var txt = document.getElementById("txtLoginID");
        txt.focus();
    </script>

    </form>
</body>
</html>
Posted
Updated 2-Nov-21 1:23am

You can simplify your code by using the AppSettingsExpressionBuilder class[^]:
JavaScript
var key1 = '<%$ AppSettings:AESKey %>';
Since you're no longer using the <%= ... %> construct, this should also resolve the error.

NB: You need to be absolutely sure that the appSettings value will not contain a single-quote character ('); otherwise, your Javascript will not be valid.
 
Share this answer
 

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