开发者

Overwrite Meta Tags from Master Page with Content Page meta tags

In the code behind of the master Page I create the meta tags:

   HtmlMeta _metaDescription = new HtmlMeta();
    _metaDescription.Name = "description";
    _metaDescription.Content = "this is the description";
    _metaDescription.ID = "metaD"; 
    this.Page.Header.Controls.Add(_metaDescription);

   HtmlMeta _metaKeywordsMaster = new HtmlMeta();
    _metaKeywordsMaster.Name = "keywords";
    _metaKeywordsMaster.Content = "here are the keywords" ;
    _metaDescription.ID = "metaK";
    this.Page.Header.Controls.Add(_metaKeywordsMaster);


   HtmlMeta _metaTitleMaster = new HtmlMeta();
    _metaTitleMaster.Name = "title";
    _metaTitleMaster.Content = "TitlePage";
    _metaDescription.ID = "metaT";
    this.Page.Header.Controls.Add(_metaTitleMaster);

If I enter a specific c开发者_开发技巧ontentpage I want to overwrite these meta tages by removing them and create new meta tags

        HtmlMeta meta = (HtmlMeta)this.Header.FindControl("ctl00metaT");
        this.Header.Controls.Remove(meta);


            HtmlMeta _metaDescription = new HtmlMeta();
            _metaDescription.Name = "description";
            _metaDescription.Content = "NewDescription";
            base.Master.Page.Header.Controls.Add(_metaDescription);
            //this.Page.Controls.Add(_metaDescription);

            HtmlMeta _metaKeywords = new HtmlMeta();
            _metaKeywords.Name = "keywords";
            _metaKeywords.Content = "NewKeywords";
            base.Master.Page.Controls.Add(_metaKeywords);
            //this.Page.Controls.Add(_metaKeywords);

            HtmlMeta _metaTitle = new HtmlMeta();
            _metaTitle.Name = "title";
            _metaTitle.Content = "NewTitle";
            base.Master.Page.Controls.Add(_metaTitle);                     

But it doesnt remove the old tags, I get double tags instead , what am I doing wrong ???


In order to overwrite the meta tag, you should write the below code on the load of the page

((System.Web.UI.HtmlControls.HtmlMeta)Page.Header.Controls[0]).Content = "IE=edge";

Of course you need to be sure to find the correct index of the control.


Here is a really simple solution, it works perfect for me:

http://lionfishtechnologies.com/developers/tips/c-sharp_add_meta_keywords_description_to_master_page.html


Follow these steps

1.Create a base page class for master page and put properties like below

public class MasterBasePage : System.Web.UI.MasterPage
        {

            private string _pageTitle;

            private string _pageDescription;
            public string PageTitle
            {
                get { return _pageTitle; }
                set { _pageTitle = value; }
            }

            public string PageDescription
            {
                get { return _pageDescription; }
                set { _pageDescription = value; }
            }

            protected override void OnLoad(EventArgs e)
            {
                if (string.IsNullOrEmpty(PageTitle))
                {
                    _pageTitle = this.Page.Title;
                }
                _pageDescription = "Select from config file";
                this.Page.Title = "Page Title";
                HtmlMeta metaTag = new HtmlMeta();
                metaTag.Name = "Description";
                metaTag.Content = _pageDescription;
                Page.Header.Controls.Add(metaTag);

                base.OnLoad(e);
            }
        }


2.Inherit your master page class from BasePage

public partial class SiteMaster : MasterBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }

3.In the content page add following attribute(replace site.master with your own)

<%@ MasterType VirtualPath="~/Site.master" %>
  1. Override the master page base properties in the content page as below

    protected void Page_Load(object sender, EventArgs e) { Master.PageTitle = "Page"; Master.PageDescription = "sadada"; }

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜