Solved

The Controls collection cannot be modified because the control contains code blocks in masterpages

Posted on 2010-08-19
5
435 Views
Last Modified: 2013-11-08
Hi, I am trying to dymanically create the metatags for the title, description and keywords. This is being done in the vb script in a page that has its own master page. I have looked at other similar issues and it seams to imply that this error is because there is some java script in the header section and this should be moved to the page load section, which I cannot see how to do.
My master page header looks like this:

<head id="Head1" runat="server">
        <asp:ContentPlaceHolder id="head" runat="server">
    </asp:ContentPlaceHolder>
       <script type="text/javascript">
        function RefreshPage() {
            var pgeId = '<%= Page.ClientID %>';
            _dopastBack(pageId, 'Myargs');
        }
        function showall() {
            var sPath = window.location.pathname;
            var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
            window.location.href = sPage;
        }
</script>

I then have a child page:
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>

and in the onpage load in the VB script for the page have:
Dim HeadTitle As New HtmlMeta
        HeadTitle.Name = "Title"
        HeadTitle.Content = "This page is about - " + myTable(0)("Description")
        Header.Controls.Add(HeadTitle)

But I get the error message:
The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
on the line:
Header.Controls.Add(HeadTitle)
any suggestions to work arround this ?
0
Comment
Question by:Simon Cripps
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 9

Expert Comment

by:puru1981
ID: 33473178
are you using scriptmanager then try the below

Replace all your <%=  %> tags with <%#  %>

0
 
LVL 9

Accepted Solution

by:
puru1981 earned 500 total points
ID: 33473207
0
 

Author Comment

by:Simon Cripps
ID: 33473273
As far as I know I'm not using script manager and I also can't see any place where I'm using <%=  %>.
Are these implied when you use content placeholders?
So I can't find which bits to fix from your suggested solution. Appologies for my ignorance
0
 
LVL 9

Expert Comment

by:puru1981
ID: 33473738
yes,

have you visited the link above.

it should solve your problem

this occurs mostly when something is not within form tag. which is in your case you are trying to write something in header.

here you can see that the function you are writing is in head which is outside the form tag.

It would be better if you can write the title, metatags and descriptions in codebehind.
MasterPage master= (MasterPage)this.Master;

            master.Keywords = "keywords";
            master.Description = "Description";
            this.Title = "title";

Open in new window

0
 

Author Closing Comment

by:Simon Cripps
ID: 33483663
Many thanks have applied what the article was implying and the issue is now resolved.
Cheers
0

Featured Post

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
asp.net repeater server posting 5 36
How do I "share" on social sites? 2 37
Adding Rows to Grid 4 31
VB.NET Parsing UDP Bytes 15 35
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question