Server Control Remove Span

Normally when you write a server control, .Net automatically wraps the output with a span and the Id of the control. Depending on your CSS this can cause rendering issues. You can use the following technique to prevent the span from being rendered.

public override void RenderBeginTag(HtmlTextWriter writer)
{
    using (HtmlTextWriter blankOne = new HtmlTextWriter(new System.IO.StringWriter()))
    {
        base.RenderBeginTag(blankOne);
    }
}
public override void RenderEndTag(HtmlTextWriter writer)
{
}

It basically writes out the open tag to a new HTML Writer and therefore is not part of the final rendered HTML


One Response to “Server Control Remove Span”

  1. Thank you. This is a short but effective way of getting rid of that annoying set of span tags. In my case it allowed me to generate html which would validate.

Leave a Reply