ASP.NET: HtmlHead ControlAdapters implementation
Here I want to show my implementation of ControlAdapters to render HtmlHead element properly in ASP.NET (at least as far as I can see it :)). For reference see my previous entries on the issue:
And here is original implementation of Control Adapters by Bazile.
So in order to render HtmlHead need to implement the following Control Adapters and register them in Default.browser:
- HtmlHeadAdapter
public class HtmlHeadAdapter : System.Web.UI.Adapters.ControlAdapter { protected override void Render(HtmlTextWriter writer) { writer.WriteLine("<head>"); HtmlHead headTag = (HtmlHead)this.Control; foreach(Control contrl in headTag.Controls) { contrl.RenderControl(writer); } writer.WriteLine("</head>"); } }
- HtmlTitleAdapter
public class HtmlTitleAdapter : System.Web.UI.Adapters.ControlAdapter { protected override void Render(HtmlTextWriter writer) { // Title writer.Write("<title>"); writer.Write(Page.Title); writer.WriteLine("</title>"); } }
- HtmlMetaAdapter
public class HtmlMetaAdapter : System.Web.UI.Adapters.ControlAdapter { protected override void Render(HtmlTextWriter writer) { HtmlMeta metaTag = (HtmlMeta)this.Control; writer.Write("<meta"); if (!String.IsNullOrEmpty(metaTag.HttpEquiv)) { writer.Write(" http-equiv=\""); writer.Write(metaTag.HttpEquiv); writer.Write("\""); } if (!String.IsNullOrEmpty(metaTag.Name)) { writer.Write(" name=\""); writer.Write(metaTag.Name); writer.Write("\""); } writer.Write(" content=\""); writer.Write(metaTag.Content); writer.Write("\""); writer.WriteLine(" />"); } }
- HtmlLinkAdapter
public class HtmlLinkAdapter : System.Web.UI.Adapters.ControlAdapter { protected override void Render(HtmlTextWriter writer) { HtmlLink linkTag = (HtmlLink)this.Control; if ((linkTag.Attributes != null) && (linkTag.Attributes.Count > 0)) { writer.Write("<link"); foreach (string key in linkTag.Attributes.Keys) { writer.Write(" "); writer.Write(key); writer.Write("=\""); writer.Write(linkTag.Attributes[key]); writer.Write("\""); } writer.WriteLine(" />"); } } }
Wednesday, January 31, 2007 1:51 AM