Click here to Skip to main content
14,599,188 members
Rate this:
Please Sign up or sign in to vote.
See more:
hello every one,

i am trying to add metatag to master page header from content page. My code goes like this.
string sn = ds.Tables[0].Rows[0]["MetaTitle"].ToString();
Literal lmeta = new Literal();
lmeta.Mode = LiteralMode.PassThrough;
lmeta.Text = string.Format("<meta name=\"Title\" content=\"{0}\"/>", sn);
this.Page.Header.Controls.Add(lmeta);
this.Page.Title = sn;

exactly at this line
this.Page.Header.Controls.Add(lmeta);

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


i googled it but didn't solution for it.

My master page header goes like below.

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="mMaster.master.cs" Inherits="m_mMaster" %>

<!DOCTYPE html>
<html lang="en">
<head id="Header"  runat="server">
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
    <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
    <link href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.1.0/css/font-awesome.min.css"

        rel="stylesheet" type="text/css" />
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" />
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap-theme.min.css" />
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"


even though if i put meta tag binding code in master page i am getting the same error. Please help me.
Posted
Updated 29-Dec-14 19:48pm
v3
Rate this:
Please Sign up or sign in to vote.

Solution 1

Hi, there are several ways to do this. Try this one

protected void Page_Load(object sender, EventArgs e)
{
//Add Page Title
//this.Page.Title = "DYNAMIC PAGE TITLE HERE";

//Add Keywords Meta Tag
HtmlMeta keywords = new HtmlMeta();
keywords.HttpEquiv = "keywords";
keywords.Name = "keywords";
keywords.Content = "ENTER YOUR FORMATED CONTENT";
this.Page.Header.Controls.Add(keywords);
//Add Description Meta Tag
HtmlMeta description = new HtmlMeta();
description.HttpEquiv = "description";
description.Name = "description";
description.Content = "ENTER YOUR FORMATED CONTENT";
this.Page.Header.Controls.Add(description);
}

Remember to ACCEPT solution if it works for you
   
Comments
Navin.Paruchuri 30-Dec-14 1:45am
   
hello tegaton.. thanks for the reply. I have tried your code but same error. this.Page.Header.Controls.Add(description); is giving me the error "The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)."
[no name] 30-Dec-14 2:00am
   
Remove all your previous codes and html content and give it a go.
[no name] 30-Dec-14 2:08am
   
Better still, view this tip and download the DEMO. http://aspsnippets.com/Articles/Dynamically-add-meta-tags-in-ASPNet.aspx
Navin.Paruchuri 2-Jan-15 5:14am
   
thanks guys... i had to remove all <% %> tags in my header to get rid of this error.
Rate this:
Please Sign up or sign in to vote.

Solution 2

   
Comments
Navin.Paruchuri 2-Jan-15 5:15am
   
thanks man... i solved my problem in another way. instead of using place holder i removed all <% %> tags in my master page header.

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100