简体   繁体   中英

RenderControl method not rendering self-closing tags

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.

我怀疑这行不会执行,或者抛出异常并吞下不允许它完成:

<%# ((Image)Container.DataItem).Description %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM