Compartilhar via


Literal Web Server Control Overview

You can use a Literal Web server control as a container for other content on the page.

This topic contains:

  • Scenarios

  • Background

  • Code Examples

  • Class Reference

Scenarios

The Literal control is used most frequently when adding content dynamically to the page.

Back to top

Background

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

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 a span element.) As a consequence, the Literal control does not support any style attributes, including position attributes. However, the Literal control enables you to specify whether content is encoded.

The Panel and PlaceHolder controls render as div elements, which create discrete blocks in the page, unlike rendering in-line the way the Label and Literal controls do.

In general, use a Literal control when you want to render text and controls directly into a page without any additional markup.

Encoding Content in the Literal Control

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

  • Transform. Any markup that you add to the control is transformed to accommodate the protocol of the requesting browser. This setting is useful if you are rendering content to mobile devices that use protocol other than HTML.

  • PassThrough. Any markup that you add to the control is rendered as-is to the browser.

  • Encode. Any markup that you add to the control is encoded using the HtmlEncode method, which converts HTML encoding into its text representation. For example, a <b> tag is rendered as &lt;b&gt;. Encoding is useful when you want the browser to display markup rather than interpret it. Encoding is also useful for security, to help prevent malicious markup from being executed in the browser. It recommended if you are displaying strings from an untrusted source. For more information, see How to: Protect Against Script Exploits in a Web Application by Applying HTML Encoding to Strings.

Code Examples

How to: Add Literal Web Server Controls to a Web Forms Page

Class Reference

The following table lists the classes that relate to the Literal control.

Member

Description

Literal

The main class for the control.

Back to top

See Also

Reference

Label Web Server Control Overview