Prevent wrapping <span> tags for ASP.NET server control Prevent wrapping <span> tags for ASP.NET server control asp.net asp.net

Prevent wrapping <span> tags for ASP.NET server control


What about this?

    public override void RenderBeginTag(HtmlTextWriter writer)    {        writer.Write("");    }    public override void RenderEndTag(HtmlTextWriter writer)    {        writer.Write("");    }


A more elegant way to do this is by using the contrustor of WebControl (By default this is called with the HtmlTextWriterTag.Span)

public MyWebControl() : base(HtmlTextWriterTag.Div){}

and override the RenderBeginTag method to add custom attributes or other stuff:

public override void RenderBeginTag(HtmlTextWriter writer)    {        writer.AddAttribute("class", "SomeClassName");        base.RenderBeginTag(writer);    }


I was experiencing the same issue. In my case I was overriding the methods:

protected override void OnPreRender(EventArgs e)    { /* Insert the control's stylesheet on the page */ }

and

protected override void RenderContents(HtmlTextWriter output)        { /* Control rendering here, <span> tag will show up */ }

To prevent this, I simply replaced the RenderContents override with the following:

protected override void Render(HtmlTextWriter output)        { /* Control rendering, no <span> tag */ }

Hope this helps.