I’m new to writing custom ASP.NET server controls, and I’m encountering the following issue:
I have a control that inherits from System.Web.UI.HtmlControls.HtmlGenericControl. I override the control’s Render method, use the HtmlTextWriter to emit some custom HTML (basically a TD tag with some custom attributes), and then call the case class’ Render method.
Using the control:
<dc:Header id="header1" runat="Server" DataColumn="MemberNumber" Width="30%">Member Number</dc:Header >
The problem is that when I view my rendered HTML, the server tag is emitted to the client as well (right after the TD tag):
<dc:Header id="ctl00_ContentPlaceHolder_testData1_testData1_header1">Member Number</dc:Header>
How do I prevent this from happening?
The base render method emits the tagnames in
RenderBeginTagandRenderEndTag(), just don’t call it if you’re doing your own rendering. I also wouldn’t inherit fromHtmlGenericControlif you can help it, just inherit fromWebControlorControleven if you need none of theWebControlattributes.The normal
Render()method does this:As long as you call what you need, probably
RenderContents()in your case, no need to callbase.Render(writer).If you still want to override
HtmlGenericControlbe sure to set theTagNameproperty.