There is a strange problem with the RenderControl method.
I have a UserControl (ASCX file) with this add-in:
<ul> <asp:Repeater ID="rptImages" runat="server"> <ItemTemplate> <li> <a href="<%# ((Image)Container.DataItem).Url %>"> <img src="<%# ((Image)Container.DataItem).Url %>?mw=80&mh=50" title="<%# ((Image)Container.DataItem).Title %>" alt="<%# ((Image)Container.DataItem).Alt %>" /> <p><%# ((Image)Container.DataItem).Description %></p> </a> </li> </ItemTemplate> </asp:Repeater> </ul>
When this code runs in the normal page life cycle (for example, when it is added to the page), it displays the actual XHTML as markup:
<ul> <li> <a data-fullscreen="/someimage.jpg" href="/another-image.jpg"> <img src="/myimage?mw=80&mh=50" title="Image Title" alt="Alt Text" /> <p></p> </a> </li> </ul>
Note that the p tag has a closing tag (although it is empty), and the image tag also has a closing tag.
When I create an instance of this control on the server and try to parse it into a string using the RenderControl () method, for example:
StringBuilder builder = new StringBuilder(); using (StringWriter writer = new StringWriter(builder)) { using (XhtmlTextWriter htmlWriter = new XhtmlTextWriter(writer)) { var control = (GalleryControl)LoadControl("~/layouts/Controls/Gallery/GalleryControl.ascx"); control.Images = m_images; control.RenderControl(htmlWriter); } } return builder.ToString();
Then the returned XHTML looks like this:
<ul> <li> <a data-fullscreen="/someimage.jpg" href="/another-image.jpg"> <img src="/myimage?mw=80&mh=50" title="Image Title" alt="Alt Text"> <p> </a> </li> </ul>
Note that the image tag does not have its own closing tag, and the p tag also does not close, which makes this XHTML no longer valid.
I spent the whole day on this. I tried XhtmlTextWriter instead of HtmlTextWriter to go to RenderControl, but that didn't make any difference.
Has anyone else encountered this problem? This is pretty weird, and many of us on the team are at an impasse at the moment! Any help or ideas would be appreciated.
EDIT:
I probably should have mentioned that this code runs on the Sitecore stack. It runs on the renderField stack immediately before the ExpandLinks processor.