Differences between Literal and Label controls in ASP. NET

  • 2021-07-18 07:44:29
  • OfStack

1. Programme and background

The Literal control represents one of several options for adding content to a page. For static content, tags can be added directly to the page as HTML without using a container. However, if you want to add content dynamically, you must add it to the container. Typical containers are Label controls, Literal controls, Panel controls, and PlaceHolder controls.

The Literal control differs from the Label control in that the Literal control does not add any HTML elements to the text. (The Label control renders 1 span element.) Therefore, the Literal control does not support any style properties, including position properties. However, the Literal control allows you to specify whether to encode the content.

The Panel and PlaceHolder controls are rendered as div elements, which creates discrete blocks in the page, unlike the way the Label and Literal controls are rendered inline.

Typically, you can use the Literal control when you want text and controls to be rendered directly on the page without any additional markup.

Encoding Content in Literal Controls

The Literal control supports the Mode property, which specifies how the control handles the markup you add. You can set the Mode property to the following values:

ES 39EN. Any tags added to the control will be transformed to fit the protocol of the requesting browser. This setting is useful if you are rendering content to a mobile device that uses a protocol other than HTML.

• ES 43EN. Any tags added to the control will be rendered as they are in the browser.

Encode. Any markup added to the control is encoded using the HtmlEncode method, which converts the HTML encoding to its textual representation. For example, < b > Tag will be rendered as & lt;b & gt; . Encoding is useful when you want the browser to display tags without interpreting them. Coding is also useful for security and helps prevent malicious tags from being executed in browsers. This setting is recommended when displaying strings from untrusted sources.

2. What's the difference between Literal and Label

label uses html's when it is converted to a client web page after being processed by the server < span > Marked as a live control, while Literal is marked with nothing.

For example: < span id="Label1" > Label < /span > (Client code for label)
< b > This site < /b > (Client code for Literal)

literal cannot use styles. Positioning layout is troublesome. You can add an Literal Web server control to the page when you want to programmatically set the text without adding additional HTML tags. The Literal control is useful when you want to dynamically add text to a page without adding any elements that are not part of the dynamic text. For example, you can use the Literal control to display HTML read from a file or stream. If you want to display static text, you can render it using HTML; The Literal control is not required. Use the Literal control only when you need to render text programmatically.

3. How to: Add Literal Web Server Controls to Web Forms Pages

You can add an Literal Web server control to the Web Forms page when you want to programmatically set the text without adding additional HTML tags. The Literal control is a useful way to dynamically add text to a page without adding any elements that are not part of the dynamic text. For example, you can use the Literal control to display the HTML that you read from a file or stream.

Note: If you want to display static text, you can use HTML to render it; The Literal control is not required. Use the Literal control only if you need to dynamically change the contents of the server code.

1. From the Standard tab of the Toolbox, drag the Literal control onto the page.

2. Alternatively, in the Properties window, under the Behaviors category, set the Mode property to Transform, PassThrough, or Encode. The Mode property specifies how the control handles any tags added to it. The following example shows a simple Web page that displays headline news at run time. The body of the page (including the Literal control) is similar to the following code.


<body>
  <form runat="server">
    <h1><asp:Literal id="Headline" runat=server mode="PassThrough"/></h1>
  </form>
</body>

3. Add code to the page to set the Text property of the control at run time.

The following example shows how to programmatically set the text and encoding of an Literal control. This page contains a set of radio buttons that allow users to choose between encoded text and passed text.

Note: If you are setting the Text property to text from an untrusted source, set the Mode property of the control to Encode so that the tag does not form an executable tag.


<%@ Page Language="C#" %>
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        Literal1.Text = "This <b>text</b> is inserted dynamically.";
        if (radioEncode.Checked == true)
        {
            Literal1.Mode = LiteralMode.Encode;
        }
        if(radioPassthrough.Checked == true)
        {
            Literal1.Mode = LiteralMode.PassThrough;
        }
    }
</script>
 
<html>
<head runat="server"></head>
<body>
    <form id="form1" runat="server">
    <div>
        <br />
        <asp:RadioButton
            ID="radioEncode"
            runat="server"
            GroupName="LiteralMode"
            Checked="True"
            Text="Encode"
            AutoPostBack="True" />
        <br />
        <asp:RadioButton
            ID="radioPassthrough"
            runat="server"
            GroupName="LiteralMode"
            Text="PassThrough"
            AutoPostBack="True" />
        <br />
        <br />
        <asp:Literal ID="Literal1" runat="server"></asp:Literal>&nbsp;</div>
    </form>
</body>
</html>

4. Literal category

Keep the location on the Web page where static text is displayed.

Use the System. Web. UI. WebControls. Literal control to preserve the location where text is displayed on the Web page. The Literal control is similar to the Label control, but the Literal control does not allow styles to be applied to the displayed text. You can programmatically control the text displayed in the control by setting the Text property.

Warning: This control can be used to display user input that may contain malicious client script. Before displaying any information sent from the client in your application, check that it contains executable scripts, SQL statements, or other code. ASP. NET provides input request validation to block scripts and HTML in user input. Validation server controls are also provided to determine user input.


The following example shows how to use the Literal control to display static text.

Note: The following example uses a single-file code model, which may not work properly if copied directly into a code-behind file. This code example must be copied to an empty text file with the. aspx extension.


<%@ Page Language="C#" AutoEventWireup="True" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html>
<head>
    <title>Literal Example</title>
<script runat="server">
      void ButtonClick(Object sender, EventArgs e)
      {
         Literal1.Text="Welcome to ASP.NET!!";
      }
   </script>
</head>
<body>
   <form id="form1" runat="server">
      <h3>Literal Example</h3>
      <asp:Literal id="Literal1"
           Text="Hello World!!"
           runat="server"/>
      <br /><br />
      <asp:Button id="Button1"
           Text="Change Literal Text"
           OnClick="ButtonClick"
           runat="server"/>
   </form>
</body>
</html>


Related articles: