开发者

ASP.NET Server control emitting server side tags

开发者 https://www.devze.com 2022-12-21 03:11 出处:网络
I\'m new to writing custom ASP.NET server controls, and I\'m encountering the following issue: I have a control that inherits from Sy开发者_运维知识库stem.Web.UI.HtmlControls.HtmlGenericControl. I ov

I'm new to writing custom ASP.NET server controls, and I'm encountering the following issue:

I have a control that inherits from Sy开发者_运维知识库stem.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 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消