ASPSnippets

Alerts
Get notified when a new article is published.

Name
 
Email

Your email will always be private and will not be shared.

Follow us on twitter.
 
The Controls collection cannot be modified because the control contains code blocks (i.e. ... ).
Author Name: Mudassar Khan Published Date: June 10, 2009
Filed Under :
Issues and Exceptions
Views: 4829


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




If you like this article, help us grow by bookmarking this page on any social bookmarking site.
Bookmark and Share Page copy protected against web site content infringement by Copyscape

Related Articles

Comments

sanjib said:
your article solve my problems at last. Thanks you very much.
February 26, 2010  

stalin said:
thanks a lot
March 03, 2010  

Akshata said:
Hibr Its works in my application also.br Thanks for it.
May 14, 2010  

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.
There is no need to add BR tags. Simply press enter for new line

Name*  
Email*
Comment*  
Security code
Security code