Vishwamohan

Welcome to Vishwa's blog - Technology, Spirituality and More...

The Controls collection cannot be modified because the control

If you are using ASP.NET and JavaScript, also using some client side code to read Server side variables, you may get an error specially when you put that JavaScript Code inside Head section of the pager or if you are doing a dynamic operation such as exporting the Form’s data to Excel. Your error may look like following

Error: The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

Problem: Page is unable to read the data at client side when server variables are not yet ready.

Solution: Put a Div around your script tag and add runat=”Server”, Example is given below

<div id="Div1" runat="server">
    <script language="javascript" type="text/javascript">
 
        function DoSomething(aValue, bValue) {
            document.getElementById("<%=txtAValue.ClientID%>").value = aValue;
            document.getElementById("<%=txtBValue.ClientID%>").value = bValue;
            document.getElementById("<%=btnShowData.ClientID%>").click();
        }
 
   </script>
</div>
Loading