'Nothing from Property TagKey()
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
Solution 1:[1]
Make use of RenderBeginTag. The “RenderBeginTag” calls another method, the “AddAttributestoRender” method, to deal with all types of “attributes” related to only the opening tags. The “TagKey” corresponds to the default HTML tag corresponding to the web control. If we implement our own tags within the “RenderBegintag” method, we can simply forget about the “TagKey” issue. You can do something like this..
Public virtual void RenderBeginTag(HtmlTextWriter Writer)
{
AddAttributestoRender(writer);
HtmlTextWriterTag tagKey = TagKey;
If(tagKey != HtmlTextWriterTag.Unknown)
Writer.RenderBeginTag(tagKey);
Else
Writer.RenderBeginTag(this.tagName);
}
Solution 2:[2]
You could just override the Render() method, forcing it to render only the contents. Maybe something like this would do the trick:
public override void Render(HtmlTextWriter writer)
{
RenderContents(writer);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Peter Mortensen |
| Solution 2 | Peter Mortensen |
