Label.Text Label.Text Label.Text Label.Text Property

定義

取得或設定 Label 控制項的文字內容。Gets or sets the text content of the Label control.

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

屬性值

控制項的文字內容。The text content of the control. 預設值為 EmptyThe default value is Empty.

實作

範例

下列範例示範如何建立Label控制項的新實例,並設定其Text屬性。The following example demonstrates how to create a new instance of the Label control and set its Text property.

注意

下列程式碼範例會使用單一檔案程式碼模型,如果直接複製到程式碼後置檔案中,則可能無法正確運作。The following code sample uses the single-file code model and may not work correctly if copied directly into a code-behind file. 此程式碼範例必須複製到具有 .aspx 副檔名的空白文字檔。This code sample must be copied into an empty text file that has an .aspx extension. 如需 Web Forms 程式碼模型的詳細資訊,請參閱ASP.NET Web Form 頁面程式碼模型For more information on 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>Label Example</title>
<script language="C#" runat="server">
         void Button1_Click(Object Sender, EventArgs e) {
            Label l2 = new Label();
            l2.Text = "This is a new Label";
            l2.BorderStyle = BorderStyle.Solid;    
            Page.Controls.Add(l2);
         }
     </script>
 
 </head>
 <body>
     <h3>Label Example</h3>
     <form id="form1" runat="server">
 
         <asp:Button id="Button1" Text="Create and Show a Label" 
         OnClick="Button1_Click" 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>Label Example</title>
<script language="VB" runat="server">
         Sub Button1_Click(Sender As Object, e As EventArgs)
            Dim l2 As New Label()
            l2.Text = "This is a new Label"
            l2.BorderStyle = BorderStyle.Solid
            Page.Controls.Add(l2)
         End Sub
     </script>
 
 </head>
 <body>
     <h3>Label Example</h3>
     <form id="form1" runat="server">
 
         <asp:Button id="Button1" Text="Create and Show a Label" 
         OnClick="Button1_Click" Runat="server"/>
 
     </form>
 </body>
 </html>
   

備註

使用屬性,即可指定或決定Label控制項的文字內容。 TextUse the Text property to specify or determine the text content of the Label control. 這個屬性通常是用來以程式設計方式自訂顯示在Label控制項中的文字。This property is commonly used to programmatically customize the text that is displayed in the Label control.

Text屬性可以包含 HTML。The Text property can include HTML. 如果有,則 HTML 會原封不動地傳遞至瀏覽器,其中可以解讀為標記而不是文字。If it does, the HTML will be passed unchanged to the browser, where is might be interpreted as markup and not as text. 如果您想要讓瀏覽器以純文字顯示 HTML 標籤,您可以使用HtmlEncode方法,如類別總覽的範例所示。If you want the browser to display HTML markup as plain text, you can use the HtmlEncode method, as shown in the example for the class overview. Literal也可以使用控制項,而不是Label Mode控制項,並將屬性設定為EncodeYou can also use the Literal control instead of the Label control, and set the Mode property to Encode.

注意

設定屬性將會清除控制項中包含的Label任何其他控制項。 TextSetting the Text property will clear any other controls contained in the Label control.

重要

這個控制項可以用來顯示使用者輸入,這是潛在的安全性威脅。This control can be used to display user input, which is a potential security threat. 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。By default, ASP.NET Web pages validate that user input does not include script or HTML elements. 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。For more information, see Script Exploits Overview.

設定這個屬性的值時,可以使用設計工具,自動儲存到資源檔中。The value of this property, when set, can be saved automatically to a resource file by using a designer tool. 如需詳細資訊, LocalizableAttribute請參閱和ASP.NET 全球化和當地語系化For more information, see LocalizableAttribute and ASP.NET Globalization and Localization.

這個屬性是ControlParameter物件在執行時間系結的預設屬性。This property is the default property that a ControlParameter object binds to at run time. 如需詳細資訊, ControlValuePropertyAttributeControlParameter參閱、和搭配使用參數與資料來源控制項進行篩選For more information, see ControlValuePropertyAttribute, ControlParameter, and Using Parameters with Data Source Controls for Filtering.

適用於