{"id":449,"date":"2019-02-15T15:50:01","date_gmt":"2019-02-15T19:50:01","guid":{"rendered":"https:\/\/benjaminray.com\/codebase\/?p=449"},"modified":"2019-02-15T15:53:33","modified_gmt":"2019-02-15T19:53:33","slug":"code-blocks-in-page-header-may-cause-error-in-page-header-controls-add-in-asp-net","status":"publish","type":"post","link":"https:\/\/benjaminray.com\/codebase\/code-blocks-in-page-header-may-cause-error-in-page-header-controls-add-in-asp-net\/","title":{"rendered":"Code Blocks in Page Header May Cause Error in Page.Header.Controls.Add in ASP.Net"},"content":{"rendered":"<p>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:<\/p>\n<blockquote><p>\n  The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).\n<\/p><\/blockquote>\n<p>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).<\/p>\n<p>Always wrap any code blocks in a PlaceHolder with runat=server, as follows:<\/p>\n<pre><code>&lt;asp:PlaceHolder runat=\"server\"&gt;\n    &lt;%: \/\/ Some code here %&gt;\n&lt;\/asp:PlaceHolder&gt;\n<\/code><\/pre>\n<p>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().<\/p>\n<p>See more info here: <a href=\"https:\/\/stackoverflow.com\/a\/4995628\/4669143\" rel=\"noopener\" target=\"_blank\">https:\/\/stackoverflow.com\/a\/4995628\/4669143<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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  [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[13],"tags":[],"class_list":["post-449","post","type-post","status-publish","format-standard","hentry","category-asp-net"],"acf":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9GNjN-7f","jetpack_likes_enabled":false,"_links":{"self":[{"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/posts\/449","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/comments?post=449"}],"version-history":[{"count":5,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/posts\/449\/revisions"}],"predecessor-version":[{"id":454,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/posts\/449\/revisions\/454"}],"wp:attachment":[{"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/media?parent=449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/categories?post=449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/tags?post=449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}