使用内容页面元标记覆盖主页面中的元标记

时间:2011-05-07 18:59:57

标签: c# 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);

如果我输入特定的内容页面,我想通过删除它们并创建新的元标记来覆盖这些元标记

        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);                     

但它没有删除旧标签,我得到双标签,我做错了什么???

3 个答案:

答案 0 :(得分:1)

这是一个非常简单的解决方案,它对我来说非常适合:

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

答案 1 :(得分:1)

为了覆盖元标记,您应该在页面加载上编写以下代码

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

当然,您需要确保找到正确的控件索引。

答案 2 :(得分:0)

请按照以下步骤操作

1.为母版页创建基页类并放置如下所示的属性

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.从BasePage

中继承您的母版页类
public partial class SiteMaster : MasterBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }

3.在内容页面中添加以下属性(将site.master替换为您自己的属性)

<%@ MasterType VirtualPath="~/Site.master" %>
  1. 覆盖内容页面中的母版页基本属性,如下所示

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