Loading ...

Literal control in ASP.NET | CodeAsp.Net

Literal control in ASP.NET

 /5
0 (0votes)

In this blog I will explain Literal control in ASP.NET.

The Literal control used to display static text on a web page. Literal control is not rendered as a <span> tag like label control. The Literal control doesn’t inherit from WebControl and we can’t apply style to its content because it doesn’t have a style property. There is a Mode property of Literal control.

There are three modes available in literal control –

  1. Encode Mode – If I set Encode Mode, HTML tags and script is treated like text.
  2. PassThrough Mode – If I set PassThrough Mode, HTML tags and script is not treated like text.  The text content is rendered as is.
  3. Transform Mode – If I set Transform Mode, Text content is converted to match the markup language of the requested browser. If the markup language is HTML or XHTML, the content is pass through to the browser. For other markup languages, invalid tags are removed. 
Below I am giving an example where I used these modes in Literal control –
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="LiteralMode.aspx.cs" Inherits="LiteralMode" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Literal ID="Literal1" runat="server" Mode="Encode" ></asp:Literal><br />
        <asp:Literal ID="Literal2" runat="server" Mode="PassThrough"></asp:Literal><br />
        <asp:Literal ID="Literal3" runat="server" Mode="Transform"></asp:Literal>
    </div>
    </form>
</body>
</html>


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class LiteralMode : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Literal1.Text= "My name is <b>Mohit</b>.<script>alert('Hello World');</script>";
        Literal2.Text = "My name is <b>Mohit</b>.<script>alert('Hello World');</script>";
        Literal3.Text = "My name is <b>Mohit</b>.<script>alert('Hello World');</script>";
    }
}

Output -
Note – If you set Text property of literal control dynamically from user input, it will be a security risk. But with the help of Encode mode, we can encode the HTML and script and display its normal text.
Cheers!!
Happy coding:)

Comments (8)

   
shaitender
nice post
7/22/2010
 · 
by
   
hajan
Nice input!
7/22/2010
 · 
by
   
hajan
Nice input!
7/22/2010
 · 
by
   
vinay_jss
nice post mohit!! keep it up.
7/23/2010
 · 
by
   
mohit
Thanks all for your appreciating comments.
7/23/2010
 · 
by
   
raghav_khunger
Good one.
7/25/2010
 · 
by
   
mohit
Thank you Sir.
7/26/2010
 · 
by
   
chandradev
It is very usefull tutorial. Thanks for posting.
11/25/2010
 · 
by

Top Posts