articles

Home / DeveloperSection / Articles / Literal Control in ASP.Net

Literal Control in ASP.Net

Literal Control in ASP.Net

Pushpendra Singh 12090 20-Oct-2010

A Literal control is used to write simple text on the page, you can use server side formatting options like bold, italic, underlined.

<asp:Literal ID="Literal1" runat="server" Text="Hello"></asp:Literal>

Literal Control in ASP.Net

We cannot change its appearance as in Label .Unlike asp:label control, there is no property like BackColor, ForeColor, BorderColor, BorderStyle, BorderWidth, Height etc. of Literal control.

Difference between Label Control and Literal Control

  • The main difference is, you can apply style to a Label control where as you can not apply styles in a literal control.
  • Label control renders as span tag in a webpage while Literal Control only shows the text without any tag associated to it.
  • Literal control does not maintain its viewstate.

 ASP.NET Literal itself is a class which is present under System.Web.UI namespace.

Literal Mode property:

  • 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.
  • Encode: If you set this property then content will be encoded and sent to browser for e.g., if your  string contains <hr> tag, then your string will be converted to &lt;Hr&gt; and sent to browser.
  • Transform: If you set Mode property to Transform then the string render depends upon the type of the markup.

Code:

<asp:Literal ID="Literal1" runat="server"></asp:Literal> 
       <br />
         <asp:Literal ID="Literal2" runat="server" Mode="PassThrough"></asp:Literal>  
         <br />
           <asp:Literal ID="Literal3" runat="server" Mode="Encode"></asp:Literal>
 
protected void Page_Load(object sender, EventArgs e)
    {
        Literal1.Text = "<hr>Hello from literal";
        Literal2.Text = "<hr>hr tag enclosed with mode PassThrough";
        Literal3.Text = "<hr>hr tag enclosed with mode Encode";
 
    }

 

Literal Control in ASP.Net

 

 

 


Updated 22-Apr-2020

Leave Comment

Comments

Liked By