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>
</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.

Original:

protected override void Render(HtmlTextWriter writer)
{
  this.RenderBeginTag(writer);
  this.RenderContents(writer);
  this.RenderEndTag(writer);
}

New:

protected override void Render(HtmlTextWriter writer)
{
  this.RenderContents(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()
{ }

10 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.

  5. instagram followers

    Definitely consider that that you stated.

    Your favourite justification seemed to be on the web
    the simplest thing to bear in mind of. I say to you, I certainly get irked while folks think about issues that they plainly do not realize about.
    You controlled to hit the nail upon the top as well as outlined out the whole thing without having side-effects , people can take a
    signal. Will likely be back to get more. Thank you

Leave a Reply

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