views:

72

answers:

1

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?

+2  A: 

The base render method emits the tagnames in RenderBeginTag and RenderEndTag(), just don't call it if you're doing your own rendering. I also wouldn't inherit from HtmlGenericControl if you can help it, just inherit from WebControl or Control even if you need none of the WebControl attributes.

The normal Render() method does this:

protected override void Render(HtmlTextWriter writer)
{
   RenderBeginTag(writer);
   RenderContents(writer);
   RenderEndTag(writer);
}

As long as you call what you need, probably RenderContents() in your case, no need to call base.Render(writer).

If you still want to override HtmlGenericControl be sure to set the TagName property.

Nick Craver
Works perfectly, thanks for the explanation.
Ravish