使用内容页元标记覆盖母版页中的元标记
在母版页后面的代码中,我创建元标记:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了覆盖元标记,您应该在页面加载时编写以下代码
当然您需要确保找到控件的正确索引。
In order to overwrite the meta tag, you should write the below code on the load of the page
Of course you need to be sure to find the correct index of the control.
这是一个非常简单的解决方案,它非常适合我:
https://web.archive.org/web/20170812001633/http://lionfishtechnologies.com/developers/tips/c-sharp_add_meta_keywords_description_to_master_page.html
Here is a really simple solution, it works perfect for me:
https://web.archive.org/web/20170812001633/http://lionfishtechnologies.com/developers/tips/c-sharp_add_meta_keywords_description_to_master_page.html
请按照以下步骤操作
1.为母版页创建一个基本页类并放置如下属性
2.Inherit your master page class from BasePage
3.在内容页面中添加以下属性(将 site.master 替换为您自己的)
覆盖内容页面中的母版页基本属性为下面
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
2.Inherit your master page class from BasePage
3.In the content page add following attribute(replace site.master with your own)
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";
}