Attention: We are retiring the ASP.NET Community Blogs. Learn more >

The dark underbelly of XHTML

So this isn't really a post bashing XHTML but I do have a question that hopefully someone can help me with.

I'm implementing a BasePage class for other code-behind classes to inherit from. My BasePage class is really just responsible for accessing the database where all the meta, link, and script tags for every page are defined and writing them out between the <head> and </head> tags. No big deal really. I'm using an HtmlGenericControl so that I can use the Attributes.Add method, rather than concatenating together a bunch of string data into a LiteralControl.

Just one problem. I don't want my meta tags being rendered like <meta key="value" name="value"></meta>.
I'd like them rendered like this: <meta key="value" name="value" />

I *think* the answer lies somewhere in overriding the render method. However, I'd like to avoid overriding the render method for my entire BasePage class. I found some interesting code in the RenderBeginTag method of the HtmlTextWriter class via Reflector though:

if (type1 == HtmlTextWriter.TagType.NonClosing)
            {
                  this.writer.Write(' ');
                  this.writer.Write('/');
                  this.writer.Write('>');
            }

Can I somehow set the TagType of the HtmlTextWriter associated with rendering my HtmlGenericControl instances to "NonClosing" before...like in a PreRender event or something?

Someone please sprinkle me with some wisdom on this.

UPDATE: I got some help from someone on the CodeProject ASP.NET forum and implemented a custom class based on HtmlGenericControl

public class DCHtmlGenericControl: System.Web.UI.HtmlControls.HtmlGenericControl

{

public DCHtmlGenericControl(string TagName):base(TagName)

{

}

public DCHtmlGenericControl():base()

{

}

protected override void Render(HtmlTextWriter writer)

{

writer.WriteBeginTag(this.TagName);

this.RenderAttributes(writer);

this.RenderChildren(writer);

writer.Write(@" />");

}

}

1 Comment

  • Greetings,

    what about something like this?



    private void Test(HtmlTextWriter output) {

    output.WriteBeginTag(&quot;meta&quot;);

    output.WriteAttribute(&quot;key&quot;, &quot;keyval&quot;);

    output.WriteAttribute(&quot;value&quot;, &quot;val&quot;);

    output.Write(HtmlTextWriter.SelfClosingTagEnd);

    }

Comments have been disabled for this content.