Click here to Skip to main content
15,792,870 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:

I inspired from codeproject article " Using metatag with master pages in"

I created a web site. Which has two master pages. 1-Main Master Page, 2- Nested master page for the Main Master
I got different aspx files in my page folder. some of these pages derived from nested master Page and some from
directly Main master page.

In my main master page(i.e sitemaster1) in the head section i have many links to Css files and Jquery files and
javascript files.
you can have look below to my main master page. i'm just putting here my code up to header section only.( where i'm
facing problem).

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="siteMaster1.master.cs" Inherits="siteMaster1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
<html xmlns="">
<head runat="server">
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link href="Gcss/default.css" rel="stylesheet" type="text/css" media="screen" />
<link href="Gcss/menuStyles.css" rel="stylesheet" type="text/css" media="screen" />
<!-- slider tags -->
    <link rel="stylesheet" href="<%=ResolveClientUrl("~/Css/style.css") %>" type="text/css" media="screen"
charset="utf-8" />
    <script type="text/javascript" src="<%=ResolveClientUrl("~/js/jquery-1.2.6.js")%>"></script>
    <script type="text/javascript" src="<%=ResolveClientUrl("~/js/startstop-slider.js")%>"></script>
    <asp:ContentPlaceHolder id="head" runat="server">

    <style type="text/css">
            width: 113px;


Then after

I created a  basepage class in my App_code folder.  In which I have the following code.

private string _keywords;
    private string _description;
    public BasePage()
        // TODO: Add constructor logic here
        Init += new EventHandler(BasePage_Init);
    void BasePage_Init(object sender, EventArgs e)
        HtmlMeta key= new HtmlMeta();
    if (!string.IsNullOrEmpty(Meta_Description))
        HtmlMeta des = new HtmlMeta();
        des.Name = "description";
        des.Content = Meta_Description;
    public string Meta_Keywords
            return _keywords;
            _keywords = Regex.Replace(value, "\\s+", "");
    public string Meta_Description
            return _description;
            _description = Regex.Replace(value, "\\s+", "");
Then after My home page which is drived from Nested Master Page.
for my home page the code is like below.
home.aspx.cs  code here..
public partial class Home : BasePage
    protected void Page_Load(object sender, EventArgs e)

Home.aspx code here.
<%@ Page Language="C#" MasterPageFile="~/NestedMP1.master" AutoEventWireup="true" CodeFile="Home.aspx.cs"
Inherits="Home" Title="Home" CodeFileBaseClass="BasePage"
Meta_Keywords="Rapid, guarding"
 Meta_Description="Security Services from Rapid Response Security Services, Auckland"
 Meta_Author="kingsland and mt eden rapid response security ltd"


Ok Now the problem goes here.

When I run my HOme page I get the following error:

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
Description: An unhandled exception occurred during the execution of the current web request. Please review the
stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: The Controls collection cannot be modified because the control
contains code blocks (i.e. <% ... %>).
Source Error:
The source code that generated this unhandled exception can only be shown when compiled in debug mode. To enable
this, please follow one of the below steps, then request the URL:
1. Add a "Debug=true" directive at the top of the file that generated the error. Example:
  <%@ Page Language="C#" Debug="true" %>
2) Add the following section to the configuration file of your application:
       <compilation debug="true"/>
Note that this second technique will cause all files within a given application to be compiled in debug mode. The
first technique will cause only that particular file to be compiled in debug mode.
Important: Running applications in debug mode does incur a memory/performance overhead. You should make sure that
an application has debugging disabled before deploying into production scenario.
Stack Trace:
[HttpException (0x80004005): The Controls collection cannot be modified because the control contains code blocks
(i.e. <% ... %>).]
   System.Web.UI.ControlCollection.Add(Control child) +8678903
   BasePage.BasePage_Init(Object sender, EventArgs e) +111
   System.Web.UI.Control.OnInit(EventArgs e) +99
   System.Web.UI.Page.OnInit(EventArgs e) +12
   System.Web.UI.Control.InitRecursive(Control namingContainer) +333
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean
includeStagesAfterAsyncPoint) +378

Version Information: Microsoft .NET Framework Version:2.0.50727.4927; ASP.NET Version:2.0.50727.4927

Please help to write more easy to write meta tags in my aspx code. I'm really worried about this since 2 days.
Please solve me this problem.

Thank you.
Updated 11-Mar-11 13:54pm

in your tags

Meta_Keywords="Rapid, guarding"
 Meta_Description="Security Services from Rapid Response Security Services, Auckland"
 Meta_Author="kingsland and mt eden rapid response security ltd"

I think it is not Meta_Keywords It might be MetaKeywords. Same way remove that underscore for Meta_Decription as well. Take out the whole Meta_Author tag.

And you haven's mentioned which version your .Net framework

Improved Answer

In that case you need to remove the code blocks and add those links from code behind in the main master's page load event.

Need to give an id for the head section as <head runat="server" ID="HeadContent"> . Then in the master's page load use this code.

Use this namespace as well
using System.Web.UI.HtmlControls;

HtmlLink CSSHtmlLink = new HtmlLink();
CSSHtmlLink.Attributes.Add("rel", "Stylesheet");
CSSHtmlLink.Attributes.Add("type", "text/css");
CSSHtmlLink.Attributes.Add("media", "all");
CSSHtmlLink.Attributes.Add("href", ResolveUrl("~/Styles/site.css"));

ScriptManager.RegisterClientScriptInclude(this.Page,this.GetType(),"script1", ResolveUrl("~/js/jquery-1.2.6.js"));
ScriptManager.RegisterClientScriptInclude(this.Page, this.GetType(), "script2", ResolveUrl("~/js/startstop-slider.js"));
Share this answer
#realJSOP 12-Mar-11 7:09am    
From OP: Thank you for email.

But Can you see my base page class I declared it as meta_keywords only.. so it should be same only friend. can you suggest any other solution please..

Looking forward to you..
Thank you.
sincerely yours,
Albin Abel 13-Mar-11 10:58am    
Hi look at my improved answer. Hope that helps
and I'm using .Net 3.5 version ...visual studio 2008 pro.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900