The Controls collection cannot be modified because the control contains code blocks (i.e. ... ).
 
Author:
Filed Under: Issues and Exceptions
Published Date: Jun 10, 2009
Views: 19331
 

Abstract: Here Mudassar Ahmed Khan has explained the exception The Controls collection cannot be modified because the control contains code blocks (i.e. amplt ... ampgt). In ASP.Net Web application

Comments:  19

 



Server Error in 'ASP.Net' Application.


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

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

 

Cause

While working with one if my applications where I was adding some controls to the head section of my page. Reason, I used the following JavaScript which had the <% %> tags to get the ClientID of a control

 

<head runat="server">

    <title>Untitled Page</title>

    <script type = "text/javascript">

        function GetValue()

        {

            var txt = document.getElementById("<%=TextBox1.ClientID%>");

            alert(txt.value);

        }

    </script>

</head>

 

As you can see above I have used ASP.Net Server tags to get the ClientID of textbox hence I cannot add controls dynamically to the head section

 

Solution

Remove the part which has server tags and place it somewhere else if you want to add dynamic controls from code behind

I removed my JavaScript from the head section of page and added it to the body of the page and got it working

If anyone else got some other solution that helped him please share











Related Articles



Comments



Add comments

You can add your comment about this article using the form below. Make sure you provide a valid email address
else you won't be notified when the author replies to your comment

Please note that all comments are moderated and will be deleted if they are
  • Not relavant to the article
  • Spam
  • Advertising campaigns or links to other sites
  • Abusive content.
Please do not post code, scripts or snippets.

Name*: Required
Email*: Required
Comment*: Required
Security code*: CaptchaInvalid Security Code
  Submit