停止 asp:PlaceHolder 删除空格

发布于 2024-12-14 14:05:46 字数 660 浏览 4 评论 0原文

我遇到了 asp:PlaceHolder 的一些奇怪行为,它删除了其中 asp:Literal 之间的空格。

例如对于此代码:

<asp:PlaceHolder ID="PlaceHolder1" runat="server">
    <asp:Literal ID="Literal1" Text="aa" runat="server"></asp:Literal>
    <asp:Literal ID="Literal2" Text="bb" runat="server"></asp:Literal>
</asp:PlaceHolder>
<asp:Literal ID="Literal3" Text="cc" runat="server"></asp:Literal>
<asp:Literal ID="Literal4" Text="dd" runat="server"></asp:Literal>

输出将为aabb cc dd

有没有办法阻止它删除空格(而不是通过在文字或 Text="aa " 之间添加 &nbsp; )?

I came across some strange behavior of asp:PlaceHolder, it removes whitespaces between the asp:Literals within it.

For example for this code:

<asp:PlaceHolder ID="PlaceHolder1" runat="server">
    <asp:Literal ID="Literal1" Text="aa" runat="server"></asp:Literal>
    <asp:Literal ID="Literal2" Text="bb" runat="server"></asp:Literal>
</asp:PlaceHolder>
<asp:Literal ID="Literal3" Text="cc" runat="server"></asp:Literal>
<asp:Literal ID="Literal4" Text="dd" runat="server"></asp:Literal>

The output will be aabb cc dd.

Is there is a way to stop it from removing the whitespaces (and not by adding   between the literals or Text="aa ")?

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

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

发布评论

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

评论(3

浅暮の光 2024-12-21 14:05:47

最优雅的解决方案是简单地重写 PlaceHolder

using System.Web.UI;
using System.Web.UI.WebControls;

namespace CustomControls
{
    [ToolboxData("<{0}:Holder ID='Holder1' runat='server'></{0}:Holder>"), ControlBuilder(typeof(ControlBuilder))]
    public sealed class Holder: PlaceHolder
    {
    }
}

这里的关键是使用 ControlBuilder 而不是 PlaceHolderControlBuilder,它只是重写它以始终返回 false 表示 AllowWhiteSpaceLiterals

您可以将其用作任何其他 Web 控件:

<%@ Register TagPrefix="asp" Namespace="CustomControls" %>
<asp:Holder runat="server">...</asp:Holder>

The most elegant solution is to simply override PlaceHolder:

using System.Web.UI;
using System.Web.UI.WebControls;

namespace CustomControls
{
    [ToolboxData("<{0}:Holder ID='Holder1' runat='server'></{0}:Holder>"), ControlBuilder(typeof(ControlBuilder))]
    public sealed class Holder: PlaceHolder
    {
    }
}

The key here is using ControlBuilder instead of PlaceHolderControlBuilder, which just overrides it to always return false for AllowWhiteSpaceLiterals.

You use it as any other web control:

<%@ Register TagPrefix="asp" Namespace="CustomControls" %>
<asp:Holder runat="server">...</asp:Holder>
沉溺在你眼里的海 2024-12-21 14:05:47

我想出了两个解决方案:

第一:asp:Panel替换asp:PlaceHolder。缺点是您会得到一个包装 div

第二:文字之间放置一个空的HTML注释

<asp:PlaceHolder ID="PlaceHolder1" runat="server">
    <asp:Literal ID="Literal1" Text="aa" runat="server"></asp:Literal><!>
    <asp:Literal ID="Literal2" Text="bb" runat="server"></asp:Literal>
</asp:PlaceHolder>

,是的,是合法的HTML注释。

I came up with two solutions:

First: To replace the asp:PlaceHolder with asp:Panel. The disadvantage is that you get a wrapping div.

Second: To put an empty HTML comment between the literals

<asp:PlaceHolder ID="PlaceHolder1" runat="server">
    <asp:Literal ID="Literal1" Text="aa" runat="server"></asp:Literal><!>
    <asp:Literal ID="Literal2" Text="bb" runat="server"></asp:Literal>
</asp:PlaceHolder>

And yes, <!> is a legal HTML comment.

许仙没带伞 2024-12-21 14:05:47

我要么用 a 包围它,要么使用 asp:label 而不是文字。您可以更好地控制它的使用方式。使用跨度 - 给它一个类或一个 id,您可以将其定位为您喜欢的样子。

或者滚动你自己的文字。这不会那么难,但你可能会错过它附带的一些其他东西。

第三种选择是创建一个临时字符串,您可以将其绑定到/用作文字的输入。这将使 aa bb 具有确定的格式。

I would either surround it with a or use a asp:label rather than a literal. You would have a lot more control over how it is going to use. With the span - give it a class or an id and you can target it to make it look how you like.

Alternatively roll your own Literal. It would not be that hard but you might miss some of the other stuffs that come with it.

A third alternative is to create an interim string that you can bind to / use as an input into the literal. This would have the aa bb in an ensured format.

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