The difference between literal and label in ASP.

Source: Internet
Author: User
Tags passthrough

Excerpt from: http://www.cnblogs.com/Fskjb/archive/2010/03/19/1690233.html

First, the programme

Literal is most commonly used to dynamically add content to a page.

Second, the background

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

The difference between a Literal control and a Label control is that the Literal control does not add any HTML elements to the text. (The Label control renders a SPAN element.) Therefore, the Literal control does not support any style properties, including positional 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 create discrete blocks in the page in a way that is rendered inline with the Label and Literal controls.

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

Encode content in the Literal control

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

· Transform. Any markup added to the control will be converted to accommodate 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.

· PassThrough. Any markup that is added to the control is rendered as-is in the browser.

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

Iii. How to: Add Literal Web Server controls to a Web Forms page

When you want to programmatically set text without adding additional HTML markup, you can add Literal Web server controls to the Web Forms page. The Literal control is a useful way to add text dynamically 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 that you read from a file or stream.

Description: If you want to display static text, you can render it using HTML; Literal controls are not required. Use the Literal control only if you need to dynamically change the content in your server code.

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

2, or, under the behavior category of the Properties window, set the Mode property to Transform, PassThrough, or Encode. The Mode property specifies how the control handles any markup that is added to it. The following example displays a simple Web page that displays headlines at run time. The body of the page, including the Literal control, resembles the following code.

<body>

<form runat= "Server" >

Mode= "PassThrough"/>

</form>

</body>

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

The following example shows how to programmatically set the text and encoding of a Literal control. The page contains a set of radio buttons that allow the user to select between encoded text and passing text.

Description: If you are setting the Text property to be from an untrusted source, set the control's Mode property 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>

</script>

<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>

Four, Literal class

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

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

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

Example

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

Description: The following example uses the single-file code model, which may not work correctly if it is copied directly into the code-behind file. This code example must be copied into an empty text file that has an. 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" >

<title>literal example</title>

<script runat= "Server" >

void ButtonClick (Object sender, EventArgs e)

{

Literal1.text= "Welcome to asp.net!!";

}

</script>

<body>

<form id= "Form1" runat= "Server" >

<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>

The difference between literal and label in ASP. NET

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.