Stripping span tag from WebControl

When I started developing web controls which are created by CreateChildControls method i run into problem that this control is surrounded by span tag.

[ToolboxData("<{0}:ExtendedLabel runat=\"server\" Text=\"Label\" />")]
public class ExtendedLabel : WebControl, INamingContainer
  protected override void CreateChildControls()
    Label label = new Label();
    label.Text = this.Text;
    this.Controls.Add (label);

This example will product not so nice HTML code:

<span id=?ExtendedLabel?>
  <span name=?ExtendedLabel:Label?>Test string</span>

The first label come from RenderBeginTag method which contains by default span tag. What you can do is to override both RenderBeginTag and RenderEndTag.

protected override void RenderBeginTag(HtmlTextWriter writer)
  writer.Text = writer.Text + "<div id=\"myTag\">";
protected override void RenderBeginTag(HtmlTextWriter writer)
  writer.Text = writer.Text + "</div>";

Or remove this tag completely by overriding Render method.


protected override void Render(HtmlTextWriter writer)


protected override void Render(HtmlTextWriter writer)

In these examples you have full control over behavior of the control. These is another possibility how to change or remove the outlining tag. You can extend your web control from another class.

protected ExtendedLabel () : base()
{ }

8 thoughts on “Stripping span tag from WebControl

  1. Neha

    Thanks a lot. I was also using Mybase.Render(writer) instead of Mybase.RenderContents(writer). This article has been very helpful.

  2. passwords

    That is very interesting, You are an excessively professional blogger.

    I have joined your rss feed and look forward to in the hunt
    for extra of your great post. Also, I have shared your
    site in my social networks

  3. thailandpost

    You can definitely see your enthusiasm in the work you write.
    The arena hopes for even more passionate writers like you
    who are not afraid to mention how they believe.
    Always follow your heart.

  4. My Drone Choice

    I blog quite often and I truly appreciate your information. The article has truly peaked my interest.
    I am going to book mark your blog and keep checking for new information about
    once per week. I opted in for your RSS feed too.

Leave a Reply

Your email address will not be published. Required fields are marked *