Literal.Mode 属性

定义

获取或设置一个枚举值,该值指定如何呈现 Literal 控件中的内容。Gets or sets an enumeration value that specifies how the content in the Literal control is rendered.

public:
 property System::Web::UI::WebControls::LiteralMode Mode { System::Web::UI::WebControls::LiteralMode get(); void set(System::Web::UI::WebControls::LiteralMode value); };
public System.Web.UI.WebControls.LiteralMode Mode { get; set; }
member this.Mode : System.Web.UI.WebControls.LiteralMode with get, set
Public Property Mode As LiteralMode

属性值

LiteralMode 枚举值之一。One of the LiteralMode enumeration values. 默认值为 TransformThe default is Transform.

异常

指定的类型不是 LiteralMode 枚举值之一。The specified type is not one of the LiteralMode enumeration values.

示例

下面的代码示例演示如何设置Literal控件的Mode属性。The following code example demonstrates how to set a Literal control's Mode property. 属性最初在控件的声明性语法中设置为EncodeModeThe Mode property is initially set to Encode in the declarative syntax for the control. 加载页面后,用户可以单击按钮,将Mode属性设置为。 PassThroughAfter the page loads, the user can click a button to set the Mode property to PassThrough. 这会使Text属性的内容以不同的方式呈现。This causes the contents of the Text property to render differently.

备注

下面的示例使用单文件代码模型,如果直接复制到代码隐藏文件中,可能无法正常工作。The following example uses the single-file code model and might not work correctly if copied directly into a code-behind file. 必须将此代码示例复制到扩展名为 .aspx 的空文本文件中。This code example must be copied into an empty text file that has an .aspx extension. 有关 Web 窗体代码模型的详细信息,请参阅ASP.NET Web 窗体页代码模型For more information about the Web Forms code model, see ASP.NET Web Forms 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>

注解

使用属性指定Literal控件内容的呈现方式。 ModeUse the Mode property to specify how the contents of a Literal control should be rendered. 使用LiteralMode枚举值之一设置此属性。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 控件的内容将转换为 HTML 编码的字符串。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. Literal如果控件在支持 HTML 或 XHTML 的浏览器上呈现,则不修改控件的内容。If the Literal control is rendered on a browser that supports HTML or XHTML, the control's contents are not modified.

如果指定PassThrough,则会将Text属性的全部内容传递到设备或浏览器,而不进行任何修改。If you specify PassThrough, the entire contents of the Text property are passed to the device or browser without making any modifications. 例如,如果Literal控件的Text属性包含<hr>标记,则会将其发送到所有设备和浏览器,无论其是否受支持。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.

如果指定Encode,则在Text呈现之前,属性的内容将转换为 HTML 编码的字符串。If you specify Encode, the contents for the Text property are converted into an HTML-encoded string before rendering. 例如,如果Literal控件的Text属性包含<hr>标记,则该属性将转换为&lt;Hr&gt;并发送到设备或浏览器。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.

如果指定TransformText属性的呈现行为取决于所呈现的标记的类型。If you specify Transform, the rendering behavior of the Text property depends on the type of markup being rendered. 为支持 HTML 或 XHTML 的设备或浏览器呈现Transform PassThrough Literal控件时,指定将生成与指定相同的行为。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. 为请求浏览器呈现Text属性的所有标记和元素。All markup tags and elements for the Text property are rendered for the requesting browser.

如果为除 HTML 或 XHTML 之外的标记语言(如 WML 或 cHTML)呈现Transform 控件,可以使用值来删除不支持的标记元素。LiteralWhen 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. 在这种情况下,不会为控件Text呈现在目标标记语言中不支持的属性的任何标记语言元素。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. 例如,如果Literal控件的Text属性包含<hr>标记,则在将内容发送到 WML 设备之前,将删除该标记。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. 例如,如果Text属性包含内容<XYZ>Test</XYZ>,则<XYZ>会删除和</XYZ>标记,并向设备或浏览器发送文本 "Test"。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.

适用于

另请参阅