Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the Literal control used for and what's the difference to the Label Control in asp.net?

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

like image 432
ecleel Avatar asked Feb 04 '09 11:02

ecleel


People also ask

What is the difference between label and Literal control in asp net?

Label control can be styled i.e. its Font, Color, Font Size, etc. can be easily changed but Literal control cannot be styled as it does not use any HTML tag. Label control enables to display static text on the web page. while Literal control is used most frequently when adding content dynamically to the page.

What is Literal control in asp net?

The Literal control is used to display text; that is, it renders static text on a Web page without adding additional HTML tags. It passes content directly to the client browser unless you use the Mode property to encode the content.

Can you apply styles to a Literal control?

You cannot apply a style to a literal control. Unlike Label control, there is no property like BackColor, ForeColor, BorderColor, BorderStyle, BorderWidth, Height etc.


4 Answers

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.

like image 81
splattne Avatar answered Oct 24 '22 22:10

splattne


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.

like image 29
Kelsey Avatar answered Oct 24 '22 22:10

Kelsey


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

like image 32
andynormancx Avatar answered Oct 24 '22 23:10

andynormancx


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

like image 29
Ray Booysen Avatar answered Oct 24 '22 23:10

Ray Booysen