Asp. The difference between literal and label controls in net-base application

Source: Internet
Author: User
Tags html tags passthrough

I. Programme and BACKGROUND

The Literal control represents one of several options for adding content to a page. For static content, you can add tags 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 location attributes. However, the Literal control allows you to specify whether the content is encoded.

The Panel and placeholder controls are rendered as DIV elements, which create discrete blocks in the page in a way that is inline with the Label and Literal controls.

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

Encoding content in a 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 is converted to accommodate the requesting browser's protocol. This setting is useful if you are rendering content to a mobile device that uses other protocols outside of HTML.

· Passthrough. Any markup you add to the control is present in the browser as is.

· Encode. Any markup added to the control is encoded using the HtmlEncode method, which converts the HTML encoding to its textual representation. For example, the,<b> tag will be rendered as <b>. Encoding is useful when you want the browser to display without interpreting the tag. 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.

What's the difference between literal and label?

Label is converted to a client Web page when it is processed by the server and the HTML <span> tag is used to live the control, and literal is not marked with any tags.

Example: <span id= "Label1" >Label</span> (client code for Label)
<b> Cloud Community </b> (literal client code)

Literal can not use the style. Locating a layout is a bit cumbersome. When you want to programmatically set text without adding additional HTML tags, you can add Literal Web server controls to the page. 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 that is read from a file or stream. If you want to display static text, you can render it using HTML; you do not need to Literal the control. Use the Literal control only if you need to render the text programmatically.

Iii. How to: Add a Literal Web server control to a Web Forms page

You can add a Literal Web server control to a Web Forms page when you want to programmatically set the text without adding additional HTML tags. 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.

Note: If you want to display static text, you can render it using HTML; you do not need to Literal the control. Use the Literal control only if you need to dynamically change the content in 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 shows a simple Web page that displays headlines at run time. The body of the page, including the Literal control, is similar to the following code.

Copy Code code as follows:

<body>
<form runat= "Server" >
</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. This page contains a set of radio buttons that allow the user to choose between encoded text and passing text.

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

Copy Code code as follows:

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

<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> </div>
</form>
</body>

Four, Literal class

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

Use the System.Web.UI.WebControls.Literal control to retain the position 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 you display 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. also provides validation server controls to determine user input.


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

Description: The following example uses a single file code model that may not work correctly if it is copied directly into a code-behind file. This code example must be copied to an empty text file with an. aspx extension.

Copy Code code as follows:

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

Related Article

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.