停止 asp:PlaceHolder 删除空格
我遇到了 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 "
之间添加
)?
I came across some strange behavior of asp:PlaceHolder
, it removes whitespaces between the asp:Literal
s 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最优雅的解决方案是简单地重写
PlaceHolder
:这里的关键是使用
ControlBuilder
而不是PlaceHolderControlBuilder
,它只是重写它以始终返回false
表示AllowWhiteSpaceLiterals
。您可以将其用作任何其他 Web 控件:
The most elegant solution is to simply override
PlaceHolder
:The key here is using
ControlBuilder
instead ofPlaceHolderControlBuilder
, which just overrides it to always returnfalse
forAllowWhiteSpaceLiterals
.You use it as any other web control:
我想出了两个解决方案:
第一:用
asp:Panel
替换asp:PlaceHolder
。缺点是您会得到一个包装div
。第二:在
文字
之间放置一个空的HTML注释,是的,
是合法的HTML注释。
I came up with two solutions:
First: To replace the
asp:PlaceHolder
withasp:Panel
. The disadvantage is that you get a wrappingdiv
.Second: To put an empty HTML comment between the
literals
And yes,
<!>
is a legal HTML comment.我要么用 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.