The Controls collection cannot be modified because the control

by Vishwa 11. October 2011 11:23

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>

Tags:

.NET

Comments are closed

About Me

Me Hello,my name is
Vishwa Mohan Kumar, PMP
MCP SOA Certified Architect
I am a Software Architect. This blog is result of my experiments.

Flickr Photos

Calendar

<<  February 2012  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011

View posts in large calendar

Archive

Recent Comments

Comment RSS

Live Traffic Feed