Reputation: 447
I have three different like buttons on three different pages in my site. Is there a way to dynamically create and assign these og tags based on what page the button is on?
Here is the code I am working with:
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);
}
// ************************************************************************
}
I know it is wrong and that there appears to be different approaches, but I am just showing you what I was doing and the direction I was going. The commented out tags are just there as a guide to which ones I will need. Any help would be appreciated!
Thanks in advance!
Upvotes: 1
Views: 4753
Reputation: 6265
What if you turn your properties into an interface and then throw that interface onto the classes that use your control?
You could create an interface that contains all of the properties you are trying to get...
interface IFaceBookMeta
{
string ogTitle {get; set;}
string ogType {get; set;}
//...... and so on
}
Then apply that interface to the page that you want to host the control on.
public partial class SomePageThatHasTheControl: System.Web.UI.Page, IFaceBookMeta
Then, in that class, you can now explicitly set the properties of the interface
this.ogTitle = "A Random Title";
this.ogType = "A Type";
Now, you go to your control's code and do something like this:
//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
Doing it this way would prevent you from modifying the control's code every time you added the control to another page. Instead, you just slap the interface on the page where you want the control, set the properties.
Upvotes: 2