Display information by ASP.Net literal control

1 minute read

The literal control is similar to the Label control. Unlike the Label control, the Literal control does not render its content inside of a tag. You can use the Literal control to display text or HTML content in a browser.

Literal control does not support either CssClass or BackColor properties.

Literal control has mode property which Label has not. Three mode property of Literal control:

PassThrough: Displays the contents of the control without encoding. Encode: The contents of the control are converted to an HTML-encoded string. Transform: Unsupported markup-language elements are removed from the contents of the control. If the Literal control is rendered on a browser that supports HTML or XHTML, the control’s contents are not modified. You will be clear after watching output of the following

Write the following code in your aspx page. Here I used 4 literal. 1st one for displaying simple message from code behind. Others 3 are displayed the content in different mode of literal.

<asp:Literal ID = "ltrlMsg" runat = "server">asp:Literal>
<br />
<asp:Literal ID = "ltrPasThrough" Mode ="PassThrough" Text = "This is Mahedee Hasan" runat = "server">asp:Literal>
<br />
<asp:Literal ID = "Literal1" Mode="Encode" Text = "This is Mahedee Hasan" runat = "server">asp:Literal>
<br />
<asp:Literal ID = "Literal2" Mode = "Transform" Text = "This is Mahedee Hasan" runat = "server">asp:Literal>
<br />

protected void Page_Load(object sender, EventArgs e)
    this.ltrlMsg.Text = "Welcome to ASP.net Literal Control!";

The output of the code is look like this.

Welcome to ASP.net Literal Control!
This is Mahedee Hasan
This is Mahedee Hasanll
This is Mahedee Hasan