What is LiteralControl? Why is it used? What is LiteralControl? Why is it used? asp.net asp.net

What is LiteralControl? Why is it used?


A LiteralControl is used to inject HTML into your page at runtime.


From MSDN Literal Control represents HTML elements, text, and any other strings in an ASP.NET page that do not require processing on the server.

Also have a look at this for Literal Control usage

A label renders a <span> tag. It allows you to programmatically change thedisplay characteristics of some text you want to display. A LiteralControlrenders exactly whatever static HTML you want it to. There is no generaladvantage of using one over the other. One may be more useful than anotherin certain circumstances. For example, if you want to display static text,and make no programmatic changes to it, you might want to use aLiteralControl.


A lot of programmers who don't understand the value of semantic markup (especially those with primarily a Windows Forms background) will use a Label control or the HTML LABEL tag as a placeholder for programmatically-generated text on a page. The disadvantage to this is that Label has semantic meaning in an HTML document.

Using a literal gives you a control with an ID attribute to hook to, while allowing you to inject semantically correct markup around it.

Additionally, if you end up not pushing any text to a LABEL tag, it will still output the tag in your HTML, like so: <label></label>, whereas a Literal with no text will output nothing - much cleaner.