views:

105

answers:

2

Apologies for the incredible newbish question, but I just wanted to hear some authorities on when and where you should use a LITERAL control over a LABEL.

As I understand it, the difference is this: A LABEL can be styled via the <SPAN> tags that are added.

I personally find the addition of <SPAN> tags in my HTML to be very annoying and never actually apply styles through ASP, and so LITERALs seem to be what should be used most of the time... but I'm concerned there's other considerations or benefits to using a LABEL over it that I'm unaware of.

Is it 100% completely fine to replace any LABELs with LITERALs, provided we're not applying styles to them? Are there NO other considerations?

Thanks for any advice.

+11  A: 

Yep, the main difference is that Literal controls just render out text, but Label controls surround it with <span> tags.

So, labels can be styled easier, but if you're just inserting text, literals are the way to go. Literal controls also have a handy property Mode which governs how the text is rendered. You can have it HTML-encoded, or rendered without any changes, or have any "unsupported markup-language elements" removed.

If you're not applying any styles (e.g. by using Label's CssClass property), it will be fine to replace Label controls with Literal controls.

Graham Clark
Another good thing to know, thanks!
Django Reinhardt
So, just to be sure here, there are NO other considerations at all? I ask because you write "the main difference is...". Obviously I'm not interested in the main difference. Thanks.
Django Reinhardt
@Django: Chris Marisic's answer is really important; ASP.Net `Label` controls should always be used when you want an HTML `<label>` element. Apart from that, Labels render text between `<spans>`, Literals don't, and Literals give you greater control over how the text is rendered. There are no special events on either control, and their contents can be accessed in the code-behind in exactly the same way (through the `Text` property).
Graham Clark
Wonderful! That explains why it's called Label, too. Thanks a lot!
Django Reinhardt
+4  A: 

When you have code similar to

<asp:Label EnableViewState="false" ID="Label8" runat="server" 
        AssociatedControlID="txtEmail">Email Address:</asp:Label>

<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>

It is optimal to use a label element because it will correctly turn it into a html label element with the correct for attribute targeting your text box that if a user clicks on the label it automatically sets their cursor inside the text field.

Otherwise use the literal unless the text being wrapped in a span will be beneficial for css styling.

Chris Marisic
Yet another great tip that I wasn't aware of. Thanks!
Django Reinhardt
Yep this is the main reason I use labels myself, provides a little nicer user experience.
Shawn Steward
Absolutely. I always wondered how to replicate this little bit of HTML in ASP.Net. I'm so glad I finally understand.
Django Reinhardt