16

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

Marcel
  • 15,039
  • 20
  • 92
  • 150
ecleel
  • 11,748
  • 15
  • 48
  • 48
  • possible duplicate of [ASP.Net: Literal vs Label](http://stackoverflow.com/questions/3309398/asp-net-literal-vs-label) – Alex Angas Feb 11 '12 at 03:12

7 Answers7

21

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
  • 102,760
  • 52
  • 202
  • 249
4

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
  • 47,246
  • 16
  • 124
  • 162
2

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

andynormancx
  • 13,421
  • 6
  • 36
  • 52
2

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
  • 28,894
  • 13
  • 84
  • 111
1

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
  • 2,899
  • 3
  • 26
  • 39
  • And with using the AssociatedControlId, it will render a label tag. In my opinion the least understood control in ASP.NET – Ray Booysen Feb 04 '09 at 13:01
0
  • We can use literal control in the title tag whereas label cannot be used in the title tag
  • Label can be used to set focus on other controls like Textbox. Whereas Literal simply rander the static text on the web page
Piyush
  • 542
  • 1
  • 8
  • 17
0

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
  • 8,125
  • 10
  • 46
  • 77