Microsoft.NET

……………………………………………….Expertise in .NET Technologies

Archive for April, 2011

Literal Control in ASP.NET

Posted by Ravi Varma Thumati on April 6, 2011

In this article I will explain about the Literal Control in ASP.NET and how to use it. Will also differentiate between Label and Literal controls

What is Literal Control?

  • The Literal Control is similar to the Label Control as they both are used to display static text on a web page.
  • The Literal Control is not inherited from WebControl namespace.
  • The Literal Control doesn’t provide substantial functionality but Literal text is programmable.
  • It doesn’t add any HTML elements to the web page. This control makes it possible to add HTML code directly in the code designer window without switching to design view and clicking the HTML button to edit the HTML.
  • You cannot apply a style to a literal control.
  • Unlike Label control, there is no property like BackColor, ForeColor, BorderColor, BorderStyle, BorderWidth, Height etc. for Literal control. That makes it more powerful, you can even put a pure HTML contents into it.

When to user Literal Control?

  • Literal control is one of the rarely used controls but it is very useful.
  • Literal control is light weight control.
  • The Literal Control is useful when you want to add text to the output of the page dynamically (from the server).
  • With that you can even programmatically manipulate the Literal text from the code behind.

In short you can say:

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.

How to code Literal control in .aspx page

<asp:Literal ID=”LiteralText” runat=”server” Text=”This is example of Literal”></asp:Literal>

The above code will be rendered as:

<div>
This is example of Literal
</div>

 

Posted in 1. Microsoft.NET | Tagged: | Leave a Comment »