2011-05-07 98 views
1

在母版頁後面的代碼,我創建meta標籤:從母版頁覆蓋Meta標籤與內容頁面的meta標籤

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

如果我輸入一個特定的contentpage我想通過去除覆蓋這些元每日新聞他們和創建新的中繼標記

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

但它並沒有刪除舊的標籤,我得到雙重標籤,而是我做錯了什麼?

回答

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.Inherit從BasePage的

你的母版頁類
public partial class SiteMaster : MasterBasePage 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 

     } 
    } 

3.在內容頁面添加以下屬性(用您自己的替換site.master)

<%@ MasterType VirtualPath="~/Site.master" %> 
  1. 覆蓋在所述內容頁如下

    主頁基性質保護無效的Page_Load(對象發件人,EventArgs的) { Master.PageTitle = 「頁」; Master.PageDescription =「sadada」; }

1

爲了覆蓋meta標籤,你應該寫上的負載下面的代碼頁面

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

當然,您需要確保找到控件的正確索引。