ASP.NET Nested TextBox Bug

Well, I learned something a couple of days ago and in the process also found an "Undocumented Feature" in ASP.NET 2.0

First, I learned that you can nest an asp:TextBox inside an asp:Label control and if you then set the associatedcontrolid attribute of the Label control to the id of the TextBox control, the label will render as a label element instead of a span element.  Both are pretty cool and both are not very well known.

So far, so good.  But let’s say you want to bind your Label control to resource files so that you can produce an internationalized web site.  This is where all the trouble starts.  Doing this with the nested TextBox will produce the following error:

Unable to find control with id ‘TextBox1′ that is associated with the Label ‘Label1′.

Owch!

The only way around this right now is to un-nest the TextBox control.

You’ll also notice that the TextBox control does not render in design view when it is nested inside the Label control.  So, I expect that the people who wrote the XML validator expected this to work and the people who wrote the design time rendering and possibly the Label control, did not plan on this working.

del.icio.us Tags:

Technorati Tags:

Related Post

3 Responses to “ASP.NET Nested TextBox Bug”

  • ginji:

    You don’t have to nest the controls in order to make the label render as a label, just setting the associatedcontrolid is enough. Nesting the controls does produce valid XHTML though.

    If you want to group elements of a form together, you should use the ‘fieldset’ and ‘legend’ (X)HTML tags

    You should be using all of the above to make your forms accessible to those with visual impairments that use a screen reader.

  • Dave:

    Regarding the not having to nest:

    I said as much.

    The problem is that it works one way without internationalization and doesn’t work once you add it. And most of us are going to creat our sites without internationalization turned on first only to find out that we have to rework all of our presentation layer once we decide to add internationalization into the mix.

  • ginji:

    Hmm, having re-read it now, I see that you are talking about them separately, but it’s not overly clear that you are. I would say most developers would have the labels and elements separate, not nested, and I don’t really see any advantage in having them nested. What benefit do you see in having them nested?