When using Page.Header.Controls.Add to add something to a page header, or possibly other scenarios where you add a control to a collection, you may receive the following error message:

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

This happens when the page header (likely on the master page) contains code blocks (<% %>) as direct children. In the default Site.Master, any code blocks are placed within a PlaceHolder tag with runat=server (including the code block that renders the modernizr bundle, for example).

Always wrap any code blocks in a PlaceHolder with runat=server, as follows:

<asp:PlaceHolder runat="server">
    <%: // Some code here %>
</asp:PlaceHolder>

The output does not change, and because the code blocks are now children of the PlaceHolder (and not the Page.Header), we can use Page.Header.Controls.Add().

See more info here: https://stackoverflow.com/a/4995628/4669143