views:

2405

answers:

6

What Literal control is used for in asp.net? and What is the difference between them and Label control?

+2  A: 

It is used to display text on the page, the text that is displayed can be set at runtime via server side code.

andynormancx
A: 

It will place LITERALLY whatever text you place in it on the page. You can use it to write html, JavaScript or just plain text.

Sergio
+7  A: 

The major difference is that the Label Control adds the span tag to the text (property) you set, allowing to apply a style to it:

<span>My Label text</span>

The Literal Control allows you to render any kind of content. You can use it to render scripts, hmtl and any other type of document content. It doesn't change the string you provide in the Text property.

Note: the Label control allows you to render straight HTML too, but it puts all your text in span tags as mentioned. So, for rendering large HTML portions a Literal control is the way to go.

P.S.: In HTML there is a <label> tag. If you use the AssociatedControlId property of the Label control, it will render as HTML <label> (thanks to Ray for pointing that out.)

For example:

<asp:Label runat="server" id="FirstNameLabel" AssociatedControlId="FirstNameTextBox">
Input First Name:
</asp:Label>
<asp:Textbox runat="server" id="FirstNameTextBox" />

Will render as:

<label for="FirstNameTextbox" id="FirstNameLabel">Input first name:</label>
<input type="text" id="FirstNameTextbox" name="FirstNameTextBox" />

See also here on W3 Schools.

splattne
what about Literal? don't insert span?
ecleel
No, the Literal control writes straight the text you insert, without changing it.
splattne
Correct, the Literal control emits exactly what you set via the text property.
andynormancx
You are wrong about the Label control. The Label control will render your text as HTML just the same way as the Literal.
andynormancx
For example, this Label will quite happily display javascript alert, no encoding/escaping is done for a label control:<asp:Label id="LabelScriptTest" runat="server" ><script language="javascript">alert("boo");</script></asp:Label>
andynormancx
andynormancx: you're absolutely right. I changed my answer. It justs adds the span-tags... I corrected by answer.
splattne
Umm, the asp:label is related. Add the AssociatedControlId property and you'll get<label for=" tags rendered.
Ray Booysen
If you try a label without an AssociatedControlId, it renders a span. If you add the AssociatedControlId it renders a label tag.
Ray Booysen
Ray, you're right too. D'oh! Not my day. Modified my answer...
splattne
Ray, you deserve an upvote too :-)
splattne
Five up votes for being somewhat less that correct, I think you're having a good day ;-)
andynormancx
+2  A: 

The label control also has the AssociatedControlId property that associates the label with another control. An example of where this is useful is with a textbox control. Once these are associated, screen readers are more able to give better results.

Another example is a radio button with a label allows you to click on the label and the radiobutton will select if the AssociatedControlId property is set.

MSDN on AssoicatedControlId

Ray Booysen
+1  A: 

As splattne mentions, the label encloses its text in a span, whereas the literal is simply a placeholder. However, be careful in making assumptions about how ASP.Net controls are going to render. It can depend on the user agent that you are using. For instance, the panel control renders as a div in IE, but renders as a table with Firefox.

darasd
And with using the AssociatedControlId, it will render a label tag. In my opinion the least understood control in ASP.NET
Ray Booysen
+3  A: 

One thing also to note is if you are justing using it to display something and have no need for formatting the text use a Literal control. The ViewState is not as heavy with a Literal vs a Label control and when you have many of these on a page using ViewState it can really bloat your page size.

I always ask myself, do I need to apply a custom style or formatting? Yes, use a Label. No, use a Literal.

Kelsey