VB.net Tutorials with Examples

VB.Net Projects

VB.Net Project 1

adplus-dvertising
Literal Control in ASP.NET
Previous Home Next
  • 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.
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.

Syntax of Literal Control

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

Important Properties of Asp.NET Webserver Literal control are given below You can Set /Get these properties at Design time or at Runtime.

Mode - Enumerator - To specify how to render literal control in a web page.

Text - Accept String - Commenly Used property to Get/Set the text that you want to render in your webpage

There are three Mode enumerator available. These are

  1. PassThrough: If you set this property, then the content will not be modified and rendered as is. For eg., if string contains <hr> tag then its dependent on your browser, of how it handles <hr> tag.
  2. Encode: If you set this property then content will be encoded and sent to browser for eg., if your string contains <hr> tag, then your string will be converted to &lt;Hr&gt; and sent to browser.
  3. Transform: If you set Mode property to Transform then the string render depends upon the type of the markup.
Events available in Asp.Net Web server Literal Control are:
  • Click
  • Init
  • Load
  • PreRender
  • UnLoad

The difference between a Label Control and a Literal Control?

  1. The main difference is, you can apply style to a Label control where as you can not apply styles in a literal control.
  2. Label control renders as span tag in a webpage while Literal Control only shows the text without any tag associated to it.
  3. Literal control does not maintain its viewstate.
  4. It's always a good practice to use literal control when  you want to display a plain text in your web page. ASP.NET Literal itself is a class which is present under System.Web.UI  namespace

.aspx code:

<asp:Literal ID="Literal1" runat="server"></asp:Literal>
<asp:Literal ID="Literal2" runat="server"></asp:Literal>
<asp:Literal ID="Literal3" runat="server"></asp:Literal>

.vb Code:

 Protected Sub ImageButton1_Click(ByVal sender As Object,
 ByVal e As System.Web.UI.ImageClickEventArgs) 
 Handles ImageButton1.Click Literal1.Mode = 
 LiteralMode.Encode Literal1.Mode = 
 LiteralMode.PassThrough Literal1.Mode = 
 LiteralMode.Transform Literal1.Text = 
 "<font size=4 color=red>Literal1 with Encode property example </font>
 <script>alert(""Literal with Encode property"");</script></br></br>" 
 Literal2.Text = 
 "<font size=4 color=green>Literal2 with PassThrough property example </font>
 <script>alert(""Literal with PassThrough property"");</script></br></br>" 
 Literal3.Text = 
 "<font size=4 color=blue>Literal3 with Encode Transform example </font>
 <script>alert(""Literal with Transform property"");
 </script></br></br>" End Sub 

Output:

Previous Home Next