ASP.NET Error: The Controls collection cannot be modified because the control contains code blocks

Posted by Max | Posted in Uncategorized | Posted on 28-10-2008

0


I’ve noticed that if you try to add control to the page using Page.Controls.Add() method the following error may occur :

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

Just check if following is true on the page causing this error:

  • Your page doesn’t have <head> elements with runat="server" attribute
  • Your page content is wrapped into form tag with runat="server" attribute

Write a comment