Solved

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

Posted on 2009-05-18
3
686 Views
Last Modified: 2012-05-07
I am trying to add a meta tag programatically. However, I get the error :

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)., when trying to add. See code below.

I do not want to move the script in <%...%> in the body as it is present in all my aspx files. How can I get this to work without making any changes in the aspx page.
<%@ Page language="c#" Inherits="Adomo.Web.Login" CodeFile="Default.aspx.cs" Title="<%$ Resources:PageTitle %>" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
	<head runat="server">
		<title />
		<meta name="vs_snapToGrid" content="False" />
		<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR" />
		<meta content="C#" name="CODE_LANGUAGE" />
		<meta content="JavaScript" name="vs_defaultClientScript" />
		<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema" />
		<link href="AdomoCss.css" type="text/css" rel="Stylesheet" /><%
    /*	Add reference for language dependent stylesheet to override layout where needed for non en-US pages. Typically, column widths will be overruled where needed.
Not added for pages called with en-US or with no culture tag (admin pages). */
    if (cultureTag != null && cultureTag != "en-us")
    {
        Response.Write("<link href='AdomoCss." + cultureTag + ".css' type='text/css' rel='Stylesheet' />");
    }
		%>
</head>
 
Adding the html meta tag in code behind in OnLoad method
 
HtmlMeta meta = new HtmlMeta();
meta.HttpEquiv = "X-UA-Compatible";
meta.Content = "IE=EmulateIE7";
this.Header.Controls.Add(meta);

Open in new window

0
Comment
Question by:anshugupta_fremont
[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
  • 2
3 Comments
 
LVL 9

Assisted Solution

by:tetorvik
tetorvik earned 125 total points
ID: 24418896
There are couple of options I am aware of:

1# remove the Response.Write() and add the css link dynamically in code-behind
http://www.aspdotnetfaq.com/Faq/How-to-Programmatically-add-CSS-Stylesheet-file-to-Asp-Net-page.aspx

2# change the <%= %> to binding expression <%# %>
http://leedumond.com/blog/the-controls-collection-cannot-be-modified-because-the-control-contains-code-blocks/
0
 

Author Comment

by:anshugupta_fremont
ID: 24424028
Do I have any other option (within code behind), that doesnt require change in the aspx file (because , I will have to make this change in 10 other aspx file).

Also, can you please explain why this is happening?

Thanks in Advance
0
 
LVL 9

Accepted Solution

by:
tetorvik earned 125 total points
ID: 24425213
Apparently you are modifying HtmlHead's control collection at code-behind and at aspx page, which is not allowed. I am not aware of the exact reason why this is not allowed.

However I came across one additional hack that gets you around the issue without modifying code-behind code. You'll need to wrap your head (<%=...%>) fragmant with some placeholder (asp:PlaceHolder will do), then you're not modifying the same control collection anymore.
<asp:PlaceHolder runat="server" ID="fbar"> 
<%
  if (cultureTag != null && cultureTag != "en-us")
  {
        Response.Write("<link href='AdomoCss." + cultureTag + ".css' type='text/css' rel='Stylesheet' />");
  }
    
%>
</asp:PlaceHolder>

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Easy filter aspnet 2 44
asp.net figure out error 3 41
Create a Dropdown list with sub group 10 37
Using asp.net to develop android, ios, windows APPS 7 45
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Suggested Courses

740 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