RenderControl method not rendering self-closing tags
Asked Answered
M

1

6

Having a strange problem with the RenderControl method.

I have UserControl (an ASCX file) with this mark-up:

<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 executes in the normal page lifecycle (eg. when it is added to a page), it renders valid XHTML as mark-up:

<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 how the p tag has a closing tag (even though it is empty), and the image tag also has a closing tag.

When I instantiate this control on the server and try to parse it to a string using the RenderControl() method like this:

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 XHTML that is returned 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 how the image tag is missing its closing tag, and the p tag is also not closing, making this XHTML no longer valid.

I've spent the entire day on this. I've tried the XhtmlTextWriter instead of the HtmlTextWriter to pass into RenderControl, but this didn't make any difference.

Has anyone else ever come across this problem? It's quite bizarre and has a lot of us in the team stumped at the moment! Any help or ideas would be appreciated.

EDIT:

I probably should have mentioned that this code is being executed in the Sitecore processor stack. It runs in the "renderField" processor stack just before the ExpandLinks processor.

Moreen answered 28/9, 2011 at 15:36 Comment(6)
@Sam: The RenderControl method is inherited from the Control class.Dinnage
I've created a solution that's pretty much the same as what you've shown here and I can't reproduce the same error. The HTML that is output is valid. I assume the binding of your Repeater is occurring in that Images setter by the way? Where is that "data-fullscreen" coming from, you're definitely giving us what the builder.ToString() is returning, before any javascript meddling is taking place?Idou
Yes, I caught the text that was returned from builder.ToString() and pasted it in here. I thought there may have been something else messing with it but this is how it is returned from the StringBuilder that it is attached to the HtmlTextWriter.Moreen
Just for kicks, add a space inbetween the paragraph tag and see what happens.Stagner
Could you include the code in the UserControl, simplified if needs be? There must be some extra work going on there to add additional attributes to the <a> tag for a start, there could be something in there. I also suspect that instantiating the control via LoadControl won't be executing all the usercontrol events you might exepect, as I experienced in my demo.Idou
There aren't any events in the UserControl. It simply binds the repeaters to a List<> of objects.Moreen
B
1

I suspect this line doesn't execute or an exception is thrown and swallowed not allowing it to finish:

<%# ((Image)Container.DataItem).Description %>
Bifoliate answered 4/10, 2011 at 2:38 Comment(2)
If you're able to debug this in Visual Studio, try changing your debug settings to show stop when an Exception is throw, rather than caught; it's a good way of finding swallowed exceptions, it's in the Debug -> Exceptions options.Newlin
I enabled the exceptions but none are being thrown. Also, when binding the image list to the control, I checked the "Description" property, and it is an empty string as opposed to being null.Moreen

© 2022 - 2024 — McMap. All rights reserved.