使用内容页元标记覆盖母版页中的元标记

发布于 2024-11-05 08:49:29 字数 1737 浏览 1 评论 0原文

在母版页后面的代码中,我创建元标记:

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

但它不会删除旧标记,而是得到双标记,什么我做错了吗???

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 contentpage 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 ???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

時窥 2024-11-12 08:49:29

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

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

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

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.

如此安好 2024-11-12 08:49:29

请按照以下步骤操作

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.Inherit your master page class from 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(对象发送者, EventArgs e)
    {
    Master.PageTitle = "页面";
    Master.PageDescription = "萨达达";
    }

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";
    }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文