This documentation is archived and is not being maintained.

Label.Text Property

Gets or sets the text content of the Label control.

[Visual Basic]
Public Overridable Property Text As String
[C#]
public virtual string Text {get; set;}
[C++]
public: __property virtual String* get_Text();
public: __property virtual void set_Text(String*);
[JScript]
public function get Text() : String;
public function set Text(String);

Property Value

The text content of the control. The default value is String.Empty.

Remarks

Use the Text property to specify or determine the text content of the Label control. This property is commonly used to programmatically customize the text that is displayed in the Label control.

Note   Setting the Text property will clear any other controls contained in the Label control.
CAUTION   This control can be used to display user input, which might include malicious client script. 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 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 Controls.

Example

[Visual Basic, C#, JScript] The following example demonstrates how to create a new instance of the Label control and set its Text property.

[Visual Basic] 
<%@ Page Language="VB" AutoEventWireup="True" %>
<html>
 <head>
 
     <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 runat="server">
 
         <asp:Button id="Button1" Text="Create and Show a Label" 
         OnClick="Button1_Click" Runat="server"/>
 
     </form>
 </body>
 </html>
   

[C#] 
<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
 <head>
 
     <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 runat="server">
 
         <asp:Button id="Button1" Text="Create and Show a Label" 
         OnClick="Button1_Click" Runat="server"/>
 
     </form>
 </body>
 </html>
   

[JScript] 
<%@ Page Language="JScript" AutoEventWireup="True" %>
<html>
 <head>
 
     <script language="JScript" runat="server">
         function Button1_Click(Sender : Object, e : EventArgs) {
            var l2 : Label = 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 runat="server">
 
         <asp:Button id="Button1" Text="Create and Show a Label" 
         OnClick="Button1_Click" Runat="server"/>
 
     </form>
 </body>
 </html>
   

[C++] No example is available for C++. To view a Visual Basic, C#, or JScript example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

See Also

Label Class | Label Members | System.Web.UI.WebControls Namespace

Show: