2012-09-28 40 views
0

我在我的網站的三個不同頁面上有三個不同的按鈕。有沒有一種方法可以根據按鈕的頁面動態創建和分配這些og標籤?如何在C#中動態分配不同的Facebook og標籤?

這裏是我一起工作的代碼:

protected void Page_Load(object sender, EventArgs e) 
{ 


    // ADD META INFORMATION TO HEADER 
    HtmlHead head = (HtmlHead)Page.Header; 


    // KEYWORDS 
    HtmlMeta hm = new HtmlMeta(); 
    hm.Name = "keywords"; 
    hm.Content = this.metaKeywords; 
    head.Controls.Add(hm); 

    // DESCRIPTION 
    hm = new HtmlMeta(); 
    hm.Name = "description"; 
    hm.Content = this.metaDescription; 
    head.Controls.Add(hm); 

    // ************************************************************************ 

    //  <meta property="og:title" content="Faces of Metastatic Breast Cancer (MBC) Video Wall" /> 
    //  <meta property="og:type" content="cause" /> 
    //  <meta property="og:image" content="http://www.facesofmbc.org/images/MBC_Logo.png"/> 
    //  <meta property="og:url" content="http://bit.ly/rkRwzx" /> 
    //  <meta property="og:site_name" content="Faces of Metastatic Breast Cancer (MBC)" /> 
    //  <meta property="og:description" content="I just viewed the new Faces of Metastatic Breast Cancer (MBC) video wall. For each view, comment or share of the video wall during October, Genentech will donate $1 to MBC initiatives. Watch TODAY!" /> 
    //  <meta property="fb:admins" content="653936690"/> 

    string ogTitle = ""; 
    string ogType = ""; 
    string ogImage = ""; 
    string ogUrl = ""; 
    string ogSiteName = ""; 
    string ogDescription = ""; 
    string ogAdmins = ""; 


    if (Page.Request.Path.Contains("videoWall.aspx")) 
    { 
     hm = new HtmlMeta(); 
     hm.Attributes.Add("property", "og:title"); 
     hm.Content = "TEST OG TITLE"; 
     head.Controls.Add(hm); 

     hm = new HtmlMeta(); 
     hm.Name = "og:type"; 
     hm.Content = "TEST OG TYPE"; 
     head.Controls.Add(hm); 
    } 
    // ************************************************************************ 
} 

我知道這是錯誤的,似乎有不同的方法,但我只是顯示你我在做什麼和方向我正想。註釋掉的標籤只是作爲我需要的指南。任何幫助,將不勝感激!

在此先感謝!

回答

2

如果你把你的屬性變成一個接口,然後將該接口扔到使用你的控件的類上,該怎麼辦?

你可以創建一個包含所有你想獲取屬性的界面...

interface IFaceBookMeta 
{ 
    string ogTitle {get; set;} 
    string ogType {get; set;} 
    //...... and so on 
} 

然後該接口適用於要主機上的控件的頁。

public partial class SomePageThatHasTheControl: System.Web.UI.Page, IFaceBookMeta 

然後,在類,你現在可以明確地設置界面

this.ogTitle = "A Random Title"; 
this.ogType = "A Type"; 

現在的屬性,你去你的控件的代碼,做這樣的事情:

//This is the page that is hosting the control. 
IFaceBookMeta meta = (IFaceBookMeta)this.Page; 

hm = new HtmlMeta(); 
hm.Attributes.Add("property", "og:title"); 
hm.Content = meta.ogTitle; 
head.Controls.Add(hm); 

hm = new HtmlMeta(); 
hm.Name = "og:type"; 
hm.Content = meta.ogType; 
head.Controls.Add(hm); 
//.... and so on 

這樣做會阻止您在每次將控件添加到另一個頁面時修改控件的代碼。相反,您只需在要控制的頁面上敲擊界面,即可設置屬性。