Role of literal in Asp.net

Source: Internet
Author: User
Tags passthrough

I. Solution

Literal is most commonly used to dynamically add content to pages.

 

Ii. Background

The literal control represents one of the several options used to add content to a page. For static content, you can use tags as HTML without using containers.
Directly add to the page. However, to add content dynamically, you must add the content to the container. Typical containers include label controls, literal controls, panel controls, and
Placeholder control.

The difference between the literal control and the label control is that the literal control does not add any HTML elements to the text. (The label control presents
SPAN element .) Therefore, the literal control does not support any style attributes including position attributes. 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 on the page, which are different from the labels and literal controls for Embedded rendering.

Generally, you can use the literal control when you want text and controls to be directly displayed on the page without any additional markup.

 

Encode content in the literal control

The literal control supports the mode attribute, which is used to specify how the control handles the tags you have added. You can set the mode attribute to the following values:

· Transform. converts any tags added to the control to adapt to the protocol of the requesting browser. This setting is useful for rendering content to mobile devices that use other protocols other than HTML.

· Passthrough. Any tag added to the control will be displayed in the browser as is.

· Encode. The htmlencode method will be used to encode any tag added to the control. This will encode the html
Encoding is converted into text representation. For example, the <B> flag is displayed
& Lt; B & gt ;. Encoding is useful when you want the browser to display without interpreting the mark. Encoding is also useful for security and helps prevent malicious tags from being executed in browsers. Display
This setting is recommended for strings from untrusted sources.

 

3. How to: Add literal Web Server Control to the web form page

You can add literal Web
Server Control. The literal control is useful when you want to dynamically add text to a page without adding any elements that do not belong to the dynamic text. For example, you can use
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 present it. The literal control is not required. The literal control is used only when you need to dynamically change the content in the server code.

 

1. Drag the literal control from the "standard" tab of the toolbox to the page.

 

2. Alternatively, set the mode attribute to transform, passthrough, or
Encode. The mode attribute specifies how the control handles any tag added to it. The following example shows a simple web page that displays the title news at runtime. The subject of this page (including
Literal control) is similar to the following code.

<Body>

<Form runat = "server">

<H1> <asp: literal id = "headline" runat = Server

Mode = "passthrough"/>

</Form>

</Body>

 

3. Add the code to the page to set the text attribute of the control at runtime.

The following example shows how to set the text and encoding of the literal control programmatically. This page contains a set of radio buttons that allow users to select between the encoded text and the transmitted text.

Note: If you are setting the text attribute to text from untrusted sources, you must set the mode attribute of the control to encode so that the tag will 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>

 

<HTML>

<Head runat = "server">

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

 

Iv. Literal class

Retain the position where static text is displayed on the webpage.

Use the system. Web. UI. webcontrols. literal control to retain the position of text displayed on the webpage. Literal
Controls are similar to label controls, but literal controls cannot apply styles to displayed text. You can set text
Property to control the text displayed in the control programmatically.

Warning:
This control can be used to display user input, which may contain malicious client scripts. Before displaying any information sent from the client in the application, check whether they contain executable scripts, SQL
Statement or other code. ASP. NET provides the input request verification function to prevent users from entering scripts and HTML. The server control is also provided to verify user input.

 

Example

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

Note: The following example uses a single-file code model. If you copy it directly to a hidden code file, it may not work properly. This sample code 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

 

<Asp: literal id = "literal1"

TEXT = "Hello world !! "

Runat = "server"/>

 

<Br/>

 

<Asp: button id = "button1"

TEXT = "Change literal text"

Onclick = "buttonclick"

Runat = "server"/>

 

</Form>

</Body>

</Html>

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.