1. Literal Web Server Control Overview (from MSDN)
The Literal Web server control can be used as a container for other content on the page. Literal is most commonly used to dynamically add content to pages.
The Literal control represents one of the several options used to add content to a page. For static content, you can add tags to the page as HTML without using containers. However, to add content dynamically, you must add the content to the container. Typical containers include Label controls, Literal controls, Panel controls, and Placeholder controls.
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 a 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 markup 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 convert HTML encoding into its text representation. For example, the <B> tag is displayed as & 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. This setting is recommended when displaying strings from untrusted sources.
Ii. What is the difference between Literal and Label?
When the label is converted to a client webpage after being processed by the server, the <span> tag of html is used as the control, while the Literal tag is not included.
Example: <span id = "Label1"> Label </span> (client code of label)
<B> Li longjie </B> (Literal client code)
Literal cannot use styles. Location layout is troublesome.
You can add a Literal Web Server Control to the page when you want to set text programmatically 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 do not belong to the dynamic text. For example, you can use the Literal control to display the HTML read from a file or stream.
To display static text, you can use HTML to render it. The Literal control is not required. The Literal control is used only when the text needs to be rendered programmatically.