add stuff to Master page's <html> tags from child page in asp.net?
I am trying to add xfbml code in one of my child page and i realize that i have to add this line开发者_如何学运维:
xmlns:fb="http://www.facebook.com/2008/fbml"
to HTML section of the page like this:
<html xmlns:fb="http://www.facebook.com/2008/fbml">
But I dont want to add it to the master page because i am using like button on only ONE child page.. is there a way I can tell asp.net to add this line when that particular child page loads?
thanks.
Yes, just do the following:
First add a public property to your master page:
public partial class Master : System.Web.UI.MasterPage
{
public bool FacebookHtml;
Then put an inline check in your Master's aspx:
<html <%= FacebookHtml ? "http://www.facebook.com/2008/fbml" : "xmlns='http://www.w3.org/1999/xhtml'" %>>
Then in your content page just set it to true (remember to include the MasterType
as well in your content page):
protected void Page_Load(object sender, EventArgs e)
{
Master.FacebookHtml = true;
}
On the front end, do something like this:
<html xmlns="http://www.w3.org/1999/xhtml" <%= FBNamespace() %>>
In the backend on the masterpage, make a public property:
Private _fbNamespace As String = ""
Public Property FBNameSpace() As String
Get
Return _fbNamespace
End Get
Set(ByVal value As String)
_fbNamespace = value
End Set
End Property
And finally in the child page:
CType(Page.Master, MasterPage).FBNameSpace = "xmlns:fb=""http://www.facebook.com/2008/fbml"""
One approach could be to make your HTML tag a server tag.
<head id="headtag" runat="server">
Your child page could then execute the code:
var headtag = Master.FindControl("headtag") as HtmlGenericControl;
headtag.Attributes["xmlns:fb"] = "http://www.facebook.com/2008/fbml";
The only side effect I can see in the markup is that you wind up with the extra id
attribute in the rendered HTML; however, I suspect that won't harm anybody.
精彩评论