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>
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:
i. 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.
ii. 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 <Hr> and sent to browser.
iii. Transform: If you set Mode property to Transform then the string render depends upon the type of the markup.
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
<asp:Literal ID="Literal2" runat="server" Mode="PassThrough"></asp:Literal>
<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";