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

Last post 11-20-2008 8:36 AM by NC01. 6 replies.

Sort Posts:

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

    11-19-2008, 11:11 AM
    • Member
      68 point Member
    • RobSil
    • Member since 07-03-2008, 4:20 PM
    • Posts 136

     I keep getting the error message ....

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

     

    I am trying to access the innerhtml of a div on my page in some javascript in my head tag.  Problem is, the <head> is runat="server" because I also want to set page title dynamically.  My javascript in my head tag is...

        <script type="text/javascript" language="javascript">
            var defaultSBText = "Search for more info...";
            function alertDivHTML() {
                var elementID = "<%= mainBodyContentPlaceholderDiv.ClientID %>";

    //error is thrown.

     

    Can someone elaborate on this error and how to avoid it?  It seems like such a common task to do, accessing the CLientID of an elment in order to access it via javascript that i am assuming I am doing somethign blatantly wrong.

     

    Thanks,

    Rob

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

    11-19-2008, 12:55 PM
    Answer
    • All-Star
      74,705 point All-Star
    • NC01
    • Member since 08-26-2005, 3:33 PM
    • Posts 13,868
    • TrustedFriends-MVPs

    Try moving the JavaScript out of the <head> tags to below the </form> tag so that the elements can be created first.

    Here are some explanations and other work-arounds if that doesn't help:
    http://west-wind.com/WebLog/posts/6148.aspx
    http://www.west-wind.com/weblog/posts/5758.aspx

    There are many more that I found with a Google search:
    http://www.google.com/search?hl=en&q=The+Controls+collection+cannot+be+modified+because+the+control+contains+code+blocks&btnG=Search

    NC...

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

    11-19-2008, 1:04 PM
    • Member
      68 point Member
    • RobSil
    • Member since 07-03-2008, 4:20 PM
    • Posts 136

     Thanks NC.

     

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

    11-19-2008, 1:17 PM
    • All-Star
      74,705 point All-Star
    • NC01
    • Member since 08-26-2005, 3:33 PM
    • Posts 13,868
    • TrustedFriends-MVPs

    So did moving the JavaScript out of the <head> tags work?

    NC...

     

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

    11-19-2008, 4:22 PM
    • Member
      68 point Member
    • RobSil
    • Member since 07-03-2008, 4:20 PM
    • Posts 136

     Yes it did.  I had already done that as a work-around anyways.  THe odd thing is, some of my pages the error shows up on, others it doesn't... in both situations there is javascript methods in the <head> that use <%= myElement.CliendID %> to refer to the object for access it's value.  I'm in the middle of another project atm, once I get a chance I'll read up on those links you mentioned to see if I can expand my knowledge in this area.

     

    Thanks,

    Rob

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

    11-19-2008, 4:36 PM
    • Member
      190 point Member
    • vadambaibala
    • Member since 10-21-2008, 7:25 PM
    • Posts 82

    i faced the same issue but i did the same thing it worked i think this post made me clear why ... thanks

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

    11-20-2008, 8:36 AM
    • All-Star
      74,705 point All-Star
    • NC01
    • Member since 08-26-2005, 3:33 PM
    • Posts 13,868
    • TrustedFriends-MVPs

    I think that it is when the parser happens to try to resolve the <%= %> tags. If controls are being created at that time, you get that error.

    NC...

Page 1 of 1 (7 items)