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′.


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. Tags:

Technorati Tags:

Like this Article? Subscribe to get every article sent to your email.

Related Post

  • DotNetNuke Modules – Creating Base ModulesDotNetNuke Modules – Creating Base Modules Now that we have DotNetNuke installed into Visual Studio we can go ahead and create our first modules. Actually, creating the modules is pretty simple. But it is even easier to do it […]
  • YAGNI – You Aren’t Going To Need ItYAGNI – You Aren’t Going To Need It One of the design principles in software development is to only write what you need today.  This has taken on the moniker of YAGNI (You Aren’t Going To Need It). The question is, […]
  • DotNetNuke Modules – LinkButtonsDotNetNuke Modules – LinkButtons As you can probably already guess, localizing a LinkButton in DotNetNuke is pretty straightforward. But you'll never guess what most people miss when they add this little control to their […]
  • Mixing ASP.NET, jQuery and JSONMixing ASP.NET, jQuery and JSON I received the following question last week: I am building a web application... I have a form with server control textbox and dropdownlist... how can I send this to the […]
  • Unrecognized Tag Prefix or Device Filter ‘asp’Unrecognized Tag Prefix or Device Filter ‘asp’ One of the companies I work for recently took over a project from another vendor.  As we started to maintain the site, we noticed that we could not drag and drop controls onto the page […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer. His commitment to quality through test driven development, vast knowledge of C#, HTML, CSS and JavaScript as well as his ability to mentor younger programmers and his passion for Agile/Scrum as defined by the Agile Manifesto and the Scrum Alliance will certainly be an asset to your organization.

One Pingback/Trackback

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

  • Pingback: Dew Drop – June 21, 2013 (#1,570) | Alvin Ashcraft's Morning Dew