3

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.

Here's markup

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

6
  • Move asp:ValidationMessage out side of asp:Label control.
    – Win
    Jun 7, 2013 at 16:39
  • what if I want it really nested in the label tag.
    – Pierluc SS
    Jun 7, 2013 at 16:40
  • Does ValidationMessage control work outside of Label control? What is the reason behind placing a control inside Label control?
    – Win
    Jun 7, 2013 at 16:47
  • @Win it does work indeed. I just want the validation message span to be in the same label tag.
    – Pierluc SS
    Jun 7, 2013 at 16:49
  • Where did you get asp:ValidationMessage control? Is it a custom server control?
    – Win
    Jun 7, 2013 at 16:54

1 Answer 1

6

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.


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

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

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"

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;
         }
      }
   }
}
8
  • It is not true. Text will render before controls.
    – Win
    Jun 7, 2013 at 17:12
  • @Win: It is true. The setter on the Labels Text property starts with: if (HasControls()) Controls.Clear() Jun 7, 2013 at 17:14
  • @Win: It only works if you set the text before you add the controls. Jun 7, 2013 at 17:14
  • really good catch. Is there any way of overriding this behavior, without replacing all those Text attributes with nested literal
    – Pierluc SS
    Jun 7, 2013 at 17:21
  • 1
    @Burnzy: You might be able to get away with using the tagMapping configuration element, but that will map all Label elements to your custom Label control. Jun 7, 2013 at 17:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.