Export (0) Print
Expand All

Literal.Mode Property

Gets or sets an enumeration value that specifies how the content in the Literal control is rendered.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

public LiteralMode Mode { get; set; }
<asp:Literal Mode="LiteralMode" />

Property Value

Type: System.Web.UI.WebControls.LiteralMode
One of the LiteralMode enumeration values. The default is Transform.

ExceptionCondition
ArgumentOutOfRangeException

The specified type is not one of the LiteralMode enumeration values.

Use the Mode property to specify how the contents of a Literal control should be rendered. This property is set using one of the LiteralMode enumeration values. The following table lists the possible values.

Value

Description

PassThrough

The contents of the control are not modified.

Encode

The contents of the control are converted to an HTML-encoded string.

Transform

Unsupported markup-language elements are removed from the contents of the control. If the Literal control is rendered on a browser that supports HTML or XHTML, the control's contents are not modified.

If you specify PassThrough, the entire contents of the Text property are passed to the device or browser without making any modifications. For example, if the Text property of a Literal control contains an <hr> tag, it is sent to all devices and browsers whether it is supported or not.

If you specify Encode, the contents for the Text property are converted into an HTML-encoded string before rendering. For example, if the Text property of a Literal control contains an <hr> tag, it is converted to &lt;Hr&gt; and sent to the device or browser.

If you specify Transform, the rendering behavior of the Text property depends on the type of markup being rendered. When the Literal control is rendered for a device or browser that supports HTML or XHTML, specifying Transform produces the same behavior as specifying PassThrough. All markup tags and elements for the Text property are rendered for the requesting browser.

When the Literal control is rendered for a markup language other than HTML or XHTML, such as WML or cHTML, you can use the Transform value to remove unsupported markup elements. In this case, any markup language elements of the Text property that are not supported in the targeted markup language are not rendered for the control. For example, if the Text property of a Literal control contains an <hr> tag, the tag is removed before the content is sent to a WML device. If an unsupported tag contains content, only the tag is removed and the content is sent to the device or browser. For example, if the Text property contains the content <XYZ>Test</XYZ>, the <XYZ> and </XYZ> tags are removed, and the text "Test" is sent to the device or browser.

TopicLocation
How to: Add Literal Web Server Controls to a Web Forms PageBuilding ASP .NET Web Applications
How to: Add Literal Web Server Controls to a Web Forms PageBuilding ASP .NET Web Applications
How to: Add Literal Web Server Controls to a Web Forms Page (Visual Studio)Building ASP .NET Web Applications in Visual Studio

The following code example demonstrates how to set a Literal control's Mode property. The Mode property is initially set to Encode in the declarative syntax for the control. After the page loads, the user can click a button to set the Mode property to PassThrough. This causes the contents of the Text property to render differently.

NoteNote:

The following example uses the single-file code model and might not work correctly if copied directly into a code-behind file. This code example must be copied into an empty text file that has an .aspx extension. For more information about the Web Forms code model, see ASP.NET Web Page Code Model.

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Literal.Mode Property Example</title>
<script runat="Server">

        Sub PassThroughButton_Click(ByVal sender As Object, ByVal e As EventArgs)

            Literal1.Mode = LiteralMode.PassThrough

            Label1.Text = "The contents of the Literal.Text property " + _
                          "passed through to the browser:" 

        End Sub

   </script>
</head>
<body>
    <form id="Form1" runat="server">

        <h3>Literal.Mode Property Example</h3>        

        <asp:Label ID="Label1"
            Text="The HTML-encoded contents of the Literal.Text property:"
            runat="server">     
        </asp:Label><br /><br />

        <asp:Literal ID="Literal1"
            Mode="Encode"
            Text= "<b>bold</b><br/><i>italic</i><br/>"          
            runat="server">
        </asp:Literal>

        <hr />

        <asp:Button ID="PassThroughButton"
            Text="Pass Through Mode"
            OnClick="PassThroughButton_Click"
            runat="server">
        </asp:Button>

    </form>
</body>
</html>

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

Community Additions

ADD
Show:
© 2014 Microsoft