On the Markup of Forms
So I ran across an interesting way of marking up Html forms. I thought it was nice, so I decided to bring that into the world of ASP.NET, because there is a (easy to overcome) roadblock in using the method in traditional ASP.NET (it is odd having to distinguish between traditional and MVC...).
You can use the method as it is basically described there with the exception of the for attribute on the input elements. With ASP.NET Old you may get a mangled id because of that INamingContainer business. Fortunately, this is not really a problem.
I basically followed the pattern for a form I built just the other day. It looks like this:
Because of INamingContainer and that I’m using a masterpage with a nested masterpage in it, you get ids that are very long, as you can see in this shot of the rendered source of the page:
And notice how I solved it. The ClientID property of any control will give you the final INamingContainer’d name (assuming you look at the property late enough in the page life-cycle...another topic altogether). Now, granted, you would render the page, get the id, and put it in the label, but if the masterpage hierarchy changes or a ASP.NET version upgrade changes the way those ids are generated, well, you’re toast (and you probably wouldn’t even notice). Use the ClientID and your problems with this method are solved.
Comments
Anatoli Papirovski 2009-01-28 07:36:14
Eric, thanks for writing this straightforward explanation for ASP.NET :)
Eric 2009-01-29 08:06:06
You are certainly welcome!