asp:label不呈现子级 [英] asp:label doesn't render children
问题描述
我有一个带有嵌套自定义控件的asp:label
,它只是不呈现.我试图为Label
类型注册一个自定义WebControlAdapter
,并且在调试时我发现Controls
集合中显然没有控件,它似乎完全忽略了任何嵌套元素.
I have an asp:label
with a nested custom control and it simply doesn't render. I tried to register a custom WebControlAdapter
for Label
type and while debugging I noticed that there is apparently no control in the Controls
collection, it seems to be completely ignoring any nested elements.
这里是标记
<asp:Label ID="lbl13" runat="server" AssociatedControlID="txt13" Text="<%$ Resources:Resources, lbl13 %>">
<asp:ValidationMessage ID="vm13" runat="server" MessageFor="txt13" CssClass="field-validation-error"></asp:ValidationMessage>
</asp:Label>
有什么办法可以绕过这个问题吗?
Any idea how to bypass this problem?
推荐答案
设置Text
属性时,它将清除子控件.如果从Label
中删除Text="<%$ Resources:Resources, lbl13 %>"
,则您的子控件应呈现.
When you set the Text
property, it clears the child controls. If you remove the Text="<%$ Resources:Resources, lbl13 %>"
from the Label
, your child controls should render.
编辑
如果将Text
属性设置为静态字符串并仅添加文字内容,则标签将仅呈现文字内容:
EDIT
If you set the Text
property to a static string and add only literal content, the label will only render the literal content:
<asp:Label runat="server" Text="Hello"> World</asp:Label>
Output: World
如果将Text
属性设置为静态字符串并添加子控件,则标签将呈现文本和子控件:
If you set the Text
property to a static string and add child controls, the label will render the text and the child controls:
<asp:Label runat="server" Text="Hello">
<asp:Label runat="server" Text="World" />
</asp:Label>
Output: HelloWorld
如果使用表达式构建器设置Text
属性,则标签将仅呈现文本:
If you set the Text
property using an expression builder, the label will only render the text:
<asp:Label runat="server" Text="<%$ Resources:Resources,Hello %>">
<asp:Label runat="server" Text="World" />
</asp:Label>
Output: Localised version of "Hello"
要覆盖此行为,您将需要一个自定义的Label
控件.例如:
To override this behaviour, you'll need a custom Label
control. For example:
public class MyLabel : Label
{
public override string Text
{
get { return base.Text; }
set
{
if (HasControls())
{
Controls.AddAt(0, new LiteralControl(value));
}
else
{
base.Text = value;
}
}
}
}
这篇关于asp:label不呈现子级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!