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()
{ }

12 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

  6. Kanai

    We realize all in relation to your persuasive moderate you present powerful steps about this blog site and therefore clearly really encourage factor by various other kinds about this issue although our very own youngster is actually getting a great deal. Spend playtime with the residual percentage of 4 seasons.

  7. 21 savage type beat

    free beats download, free trap beats mp3, free rap instrumental mp3,
    royalty free beats, free trap instrumentals, free beats to download, free rap beats,
    rap instrumental download, free beats to us,freestyle beats
    download mp3, hip hop instrumental download, free hip hop beats, migos
    type beat, drake type beat,travis scott type beat, Joyner
    lucas type beat, type beat, hopsin type beat,asap rocky type beat, gunna type beat,lil baby type
    beat, young thug type beat, offset type beat,
    gucci mane type beat, future type beat, metro booming type beat, 808 mafia type beat, young scotter type beat, Cash
    money Ap type beat, free type beat, NF type beat, Free type beat download,
    type beat download mp3,high quality fee beats,rap instrumentals,

Leave a Reply to passwords Cancel reply

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