RenderControl 方法不呈现自关闭标签

发布于 2024-12-07 04:17:19 字数 2198 浏览 0 评论 0原文

RenderControl 方法有一个奇怪的问题。

我有带有此标记的 UserControl (一个 ASCX 文件):

<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>

当此代码在正常页面生命周期中执行时(例如,当它添加到页面时),它将呈现有效的 XHTML 作为标记:

<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>

请注意 p 标记如何具有一个结束标签(即使它是空的),并且图像标签也有一个结束标签。

当我在服务器上实例化此控件并尝试使用 RenderControl() 方法将其解析为字符串时,如下所示:

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();

然后返回的 XHTML 如下所示:

<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>

请注意图像标签如何缺少其结束标签和 p 标签也没有关闭,使得这个 XHTML 不再有效。

我花了一整天的时间在这上面。我尝试过使用 XhtmlTextWriter 而不是 HtmlTextWriter 传递到 RenderControl,但这没有任何区别。

还有其他人遇到过这个问题吗?这很奇怪,我们团队中的很多人现在都被难住了!任何帮助或想法将不胜感激。

编辑:

我可能应该提到此代码正在 Sitecore 处理器堆栈中执行。它在 ExpandLinks 处理器之前的“renderField”处理器堆栈中运行。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

滥情空心 2024-12-14 04:17:19

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

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

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

<%# ((Image)Container.DataItem).Description %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文