ASP.NET Label Control Is Mostly Useless

Thursday, June 12 2008 -

The more I try (fight) to make web forms that are CSS based using ASP.NET the more I find I really dislike some of the decisions that ASP.NET is making for me. One in particular is of never ending frustration.

In ASP.NET if you put a label control on a page:

<asp:Label id="label1" runat="server" Width="150px">hello from CodeVerity</asp:label>

What gets rendered is a span

<span id="label1"' width="150px">hello from CodeVerity</span>

If you look at this in IE what you see is a label that is 150px wide, which is exactly what you wanted right?

Wrong!

You see a <span> is an inline element and as such it doesn't have a width so the width attribute should be ignored. IE incorrectly applies the width. Try the same thing in Firefox and you get a web form that looks completely different.

Now you can set the span to be an inline element and that will solve the issue some of the time, but not all (Firefox still won't render the width). The issue here is that IE isn't doing the right thing. It is "helping" me by doing what I asked instead of what it is supposed to do.

When building a web form I almost always add label controls and then have to go back and tear them out.

If the label control simply rendered a standard HTML label: <label class="goodlabel">Hello from CodeVerity</label> everything would be easy.

I guess I need to create my own server control that overrides the ASP.NET one and just render what I want.

 

del.icio.us Tags: