Literal.Text 属性

定义

获取或设置在 Literal 控件中显示的标题。Gets or sets the caption displayed in the Literal control.

public:
 property System::String ^ Text { System::String ^ get(); void set(System::String ^ value); };
[System.ComponentModel.Bindable(true)]
public string Text { get; set; }
member this.Text : string with get, set
Public Property Text As String

属性值

Literal 控件中显示的标题。The caption displayed in the Literal control.

实现

属性

示例

下面的示例演示如何使用 Text 属性以编程方式更改 Literal 控件中显示的标题。The following example demonstrates how to use the Text property to programmatically change the caption displayed in the Literal control.

备注

下面的示例使用单文件代码模型,如果直接复制到代码隐藏文件中,可能无法正常工作。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="C#" AutoEventWireup="True" %>

<!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 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</h3>

      <asp:Literal id="Literal1"
           Text="Hello World!!"
           runat="server"/>

      <br /><br />

      <asp:Button id="Button1"
           Text="Change Literal Text"
           OnClick="ButtonClick"
           runat="server"/>

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


<%@ Page Language="VB" AutoEventWireup="True" %>

<!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 Example</title>
<script runat="server">

      Sub ButtonClick(sender As Object, e As EventArgs)
      
         Literal1.Text="Welcome to ASP.NET!!"
      
      End Sub

   </script>

</head>
<body>
   <form id="form1" runat="server">
      <h3>Literal Example</h3>

      <asp:Literal id="Literal1"
           Text="Hello World!!"
           runat="server"/>

      <br /><br />

      <asp:Button id="Button1"
           Text="Change Literal Text"
           OnClick="ButtonClick"
           runat="server"/>

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

注解

使用 Text 属性指定或确定 Literal 控件中显示的标题。Use the Text property to specify or determine the caption displayed in the Literal control. 这使您可以在运行时以编程方式更改标题。This allows you to programmatically change the caption at run time.

标题是 HTML 解码的,具体取决于您为此属性指定值的方式。The caption is HTML-decoded depending on how you assign a value to this property. 如果通过 Literal 控件的属性分配一个值,则在显示值之前会对其进行 HTML 解码。If you assign a value through an attribute of the Literal control, the value is HTML decoded before it is displayed. 例如,在浏览器中,<asp:Literal id="DisplayLiteral" Text="A&nbsp;B"/> 呈现为 "A B"。For example, <asp:Literal id="DisplayLiteral" Text="A&nbsp;B"/> is rendered as "A B" on the browser. 但是,如果您以编程方式或通过在控件的开始和结束标记之间放置文本来设置此属性,则不会对标题进行 HTML 解码。However, if you set this property programmatically or by placing the text between the opening and closing tags of the control, the caption is not HTML decoded. 例如,<asp:Literal id="DisplayLiteral"> A&nbsp;B </asp:Literal> 呈现为 "A B"。For example, <asp:Literal id="DisplayLiteral"> A&nbsp;B </asp:Literal> is rendered as "A B".

注意

此控件可用于显示用户输入,其中可能包括恶意客户端脚本。This control can be used to display user input, which might include malicious client script. 在应用程序中显示可执行脚本、SQL 语句或其他代码之前,请检查从该客户端发送的任何信息。Check any information that is sent from a client for executable script, SQL statements, or other code before displaying it in your application. ASP.NET 提供输入请求验证功能来阻止用户输入中的脚本和 HTML。ASP.NET provides an input request validation feature to block script and HTML in user input. 还提供验证服务器控件以评估用户输入。Validation server controls are also provided to assess user input. 有关详细信息,请参阅验证服务器控制语法For more information, see Validation Server Control Syntax.

设置此属性后,可以使用设计器工具自动将此属性的值保存到资源文件中。The value of this property, when set, can be saved automatically to a resource file by using a designer tool. 有关详细信息,请参阅 LocalizableAttribute全球化和本地化For more information, see LocalizableAttribute and Globalization and Localization.

适用于

另请参阅