.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
sritaa
Sandeep Singh
Melody Anderson
Sharad
Eminent IT

  

Literal Control

   
 
     


Introduction :

Literal Control is used to display text in a web page. This text can be assigned in design time or dynamically. It is similar to Label control. Label control is also used to display text in a web page.

Literal Control is used to display text in a web page. This text can be assigned in design time or dynamically. It is similar to Label control. Label control is also used to display text in a web page.

So what's 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.
  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.

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

Lets See an example

Your .aspx code will look like this for a WebServer Control  Literal

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

Full HTML code used in this example

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

 

<!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>Literal Example - www.dotnetspark.com</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

   

        <asp:Label ID="lblPageMessage" runat="server" Text="Literal Control Example"></asp:Label>

   

    </div>

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

    <br />

    <br />

    <asp:Literal ID="Literal2" runat="server" Mode="PassThrough"></asp:Literal>

    <br />

    <br />

    <asp:Literal ID="Literal3" runat="server" Mode="Encode"></asp:Literal>

    </form>

</body>

</html>


And to assign value to the Literal control programmatically in Page_Load  Event form the .cs file

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

        {

            Literal1.Text = "<hr>Hello from literal";

            Literal2.Text = "<hr>hr tag enclosed with mode PassThrough";

            Literal3.Text = "<hr>hr tag enclosed with mode Encode";

        }

}

Other Tutorials in the same Category:
      
  • CheckBoxList Control      
  • Button Control      
  • Calendar Control      
  • CheckBox Control      
  • DropDownList Control or Combobox      
  • FileUpload Control      
  • Hidden Field Control      
  • ImageButton Control      
  • Link Button Control      
  • ListBox Control      
  • Literal Control      
  • RadioButton Control      
  • RadioButtonList Control      
  • TextBox Control

  •  
         
     
    Return to Main Index Page


    Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend