23

I am writing various ASP.NET Server controls and am needing to remove the tags that wrap my control by default. I am aware that you can change the tag to a different tag (as in this question, How Do I Change the render behavior of my custom control from being a span) but how can you prevent it?

I am inheriting from WebControl (can also inherit from CompositeControl).

I typically get:

<span>Control output</span>

I need:

Control output

I am overriding RenderContents(HtmlTextWriter output) and the CreateChildControls() methods (across various controls). My immediate need is to address the issue using the RenderContents(HtmlTextWriter output) method.

Community
  • 1
  • 1
Program.X
  • 6,823
  • 12
  • 47
  • 81

4 Answers4

39

What about this?

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

    public override void RenderEndTag(HtmlTextWriter writer)
    {
        writer.Write("");
    }
Claudio Redi
  • 63,880
  • 13
  • 118
  • 146
  • 3
    Brilliant solution :) I actually used an empty method for both and just put a comment line explaining why they are there. Not sure if that is any different than yours but having it write a blank string seems unnecessary. – William Feb 17 '11 at 18:24
  • This helped me a lot - had to read a lot of 'good tips', before I found your perfect solution. Would give you more than one upvote if possible *g* – Thomas Krojer Jul 03 '15 at 08:08
  • I'm trying to remove the span surrounding a System.Web.UI.Web.Controls.Checkbox. This works if I inherit from that class, however, the CssClass value doesn't get applied to the rendered CheckBox control. How would I do that? – DesertFoxAZ Jun 09 '20 at 17:48
3

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);
    }
Jim Bosch
  • 57
  • 1
  • 1
    This doesn't solve the problem. While this will allow you to change the span to a different element, such as the div given in your example, it doesn't allow you to remove the outer element completely, as requested. You could pass in `HtmlTextWriterTag.Unknown` or an empty string to the call to `base()`, but while that removes the name of the element, the angle brackets for the element are still kept, so you end up with the invalid markup: `<>`. – Elezar Nov 18 '14 at 17:55
3

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.

Mike Gilmore
  • 444
  • 4
  • 4
  • 2
    Using Render instead of RenderContents works for me. This way the RenderBeginTag/RenderEndTag is never actually called – Trent May 10 '15 at 13:42
2

I don't think the accepted answer is entirely necessary. I could be wrong, but the render method calls all three:

  • RenderBeginTag
  • RenderContents
  • RenderEndTag

So you should be able to just override render and manually call RenderContents:

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

Anyone? Maybe I'm missing something. I know this thread is old but I ran into this recently that the above was my solution.

user1447679
  • 2,777
  • 5
  • 25
  • 57