Wednesday, January 07, 2009

Do Not Forget RenderEndTag()

I have been fighting for a few hours with a strange behavior in one of my ASP.NET custom controls - Flasher. This control is for displaying an image, or a flash, on a web page. How the control renders depends on whether the media assigned to it is an image or flash file.

For some reason, this control was making the page bug out when it was set to an image, and inside an UpdatePanel. Validating the XHTML, I discovered that the <div> tag was not being generated by the UpdatePanel in the spot where I thought it would. I would have assumed something was wrong with the UpdatePanel, or it was a bug in this control, but I knew that things worked fine when my Flasher control was rendering the flash markup. Only when it was rendering the image markup did this happen.

I scanned over the code that rendered the markup in Flasher.cs, and the only difference I could notice was that the flash rendering code had both RenderBeginTag() and RenderEndTag(). I had only called RenderBeginTag() in the image markup code, falsely assuming I did not need the end tag call because <img /> is a self-terminating tag. Apparently ASP.NET will still generate the <img /> tag correctly, but if you do not make the call to RenderEndTag(), then it messes up the tag matching mechanism or what-have-ya and causes controls further on down the line to get "messed up," technically speaking.

Lesson learned.

Disqus for A Nofsinger's Blog