R – Nothing from Property TagKey()

asp.netcustom-controls

I'm writing an ASP.NET custom composite control (Inherits System.Web.UI.WebControls.CompositeControl).

By default my control mark up renders surrounded by tags. I know I can over ride the property TagKey to set the return as whatever tag I want from the System.Web.UI.HtmlTextWriterTag enum.

My question: Can I make my control render without .NET adding markup around it?


UPDATE (3/2/2011) Thanks Swati for your answer. I want to show how I am solving my question now. I think I will integrate some of Swati's ideas. Specifically, AddAttributestoRender(), but I'm wondering if that is needed since the CompositeControl will lack a parent markup tag to hold the attributes.

When I don't want a containing markup tag, then I override one property & two methods from CompositeControl.

Protected Overrides ReadOnly Property TagKey() As System.Web.UI.HtmlTextWriterTag
    Get
        ' System defaults return as HtmlTextWriterTag.Span
        Return HtmlTextWriterTag.Unknown
    End Get
End Property

Public Overrides Sub RenderBeginTag(ByVal writer As System.Web.UI.HtmlTextWriter)
    If Me.TagKey <> HtmlTextWriterTag.Unknown Then
        MyBase.RenderBeginTag(writer)
    End If
End Sub

Public Overrides Sub RenderEndTag(ByVal writer As System.Web.UI.HtmlTextWriter)
    If Me.TagKey <> HtmlTextWriterTag.Unknown Then
        MyBase.RenderBeginTag(writer)
    End If
End Sub

Best Answer

Its possible to get rid of the containing tag with a CompositeControl, but its working against the way CompositeControl likes to work (see below)

The proper way, apparently, to set TagKey to whatever the main tag of your control actually is (a div, table, or whatever).

Then override AddAttributesToRender() to set the attributes you want on your wrapper tag.

The stuff you want inside the wrapping tags should be rendered by overriding the RenderContents() method.

CompositeControl inherits from WebControl, see a discussion of the user of TagKey and AddAttributesToRender() here.

Someone on GeeksWithBlogs writes about a similar issue here

To just get rid of the wrapping though, see this forum post that shows a way to override the control constructor and the RenderBeginTag and RenderEndTag methods to remove the wrapping tags.

Related Topic