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

后端 未结 7 1876
别那么骄傲
别那么骄傲 2020-11-30 02:35

I am trying to create dynamic meta tags in C# but it gives the following error:

The Controls collection cannot be modified because the control conta

7条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-30 03:36

    It's hard to tell for sure because you haven't included many details, but I think what is going on is that there are <% ... %> code blocks inside your Page.Header (which is referring to - possibly in a master page). Therefore, when you try to add an item to the Controls collection of that control, you get the error message in the title of this question.

    If I'm right, then the workaround is to wrap a tag around the <% ... %> code block. This makes the code block a child of the Placeholder control, instead of being a direct child of the Page.Header control, but it doesn't change the rendered output at all. Now that the code block is not a direct child of Page.Header you can add things to the header's controls collection without error.

    Again, there is a code block somewhere or you wouldn't be seeing this error. If it's not in your aspx page, then the first place I would look is the file referenced by the MasterPageFile attribute at the top of your aspx.

提交回复
热议问题