Partager via


Label Web Server Control Declarative Syntax 

Displays static text on a Web Forms page and allows you to manipulate it programmatically.

<asp:Label
    AccessKey="string"
    AssociatedControlID="string"
    BackColor="color name|#dddddd"
    BorderColor="color name|#dddddd"
    BorderStyle="NotSet|None|Dotted|Dashed|Solid|Double|Groove|Ridge|
        Inset|Outset"
    BorderWidth="size"
    CssClass="string"
    Enabled="True|False"
    EnableTheming="True|False"
    EnableViewState="True|False"
    Font-Bold="True|False"
    Font-Italic="True|False"
    Font-Names="string"
    Font-Overline="True|False"
    Font-Size="string|Smaller|Larger|XX-Small|X-Small|Small|Medium|
        Large|X-Large|XX-Large"
    Font-Strikeout="True|False"
    Font-Underline="True|False"
    ForeColor="color name|#dddddd"
    Height="size"
    ID="string"
    OnDataBinding="DataBinding event handler"
    OnDisposed="Disposed event handler"
    OnInit="Init event handler"
    OnLoad="Load event handler"
    OnPreRender="PreRender event handler"
    OnUnload="Unload event handler"
    runat="server"
    SkinID="string"
    Style="string"
    TabIndex="integer"
    Text="string"
    ToolTip="string"
    Visible="True|False"
    Width="size"
/>

Remarks

Use the Label control to display text in a set location on the page. Unlike static text, you can customize the displayed text by setting the Text property.

Caution noteCaution

Text is not HTML encoded before it is displayed in the Label control. This makes it possible to embed script within HTML tags in the text. If the values for the control come from user input, be sure to validate the values to help prevent security vulnerabilities.

For detailed information on the Label Web server control's properties and events, see the Label class documentation.

Example

The following example demonstrates how to create a Label control on a Web page.

NoteNote

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

<%@ 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 Button_Click(Sender As Object, e As EventArgs) 
         Label1.Text = Server.HtmlEncode(Text1.Text)
      End Sub

   </script>

</head>

<body>

   <form id="Form1" runat="server">

      <h3>Label Example</h3>

      <asp:Label id="Label1" 
                 Text="Label Control" 
                 runat="server"/>

      <p>
        
      <asp:TextBox id="Text1" 
           Text="Copy this text to the label"
           Width="200px"  
           runat="server" />

      <asp:Button id="Button1" 
           Text="Copy" 
           OnClick="Button_Click" 
           runat="server"/>
      </p>
   </form>

</body>
</html>
<%@ 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 Button_Click(Object Sender, EventArgs e) 
      {
         Label1.Text = Server.HtmlEncode(Text1.Text);
      }

   </script>

</head>

<body>

   <form id="Form1" runat="server">

      <h3>Label Example</h3>

      <asp:Label id="Label1" 
                 Text="Label Control" 
                 runat="server"/>

      <p>
        
      <asp:TextBox id="Text1" 
           Text="Copy this text to the label"
           Width="200px"  
           runat="server" />

      <asp:Button id="Button1" 
           Text="Copy" 
           OnClick="Button_Click" 
           runat="server"/>
      </p>

   </form>

</body>
</html>

See Also

Reference

Label

Other Resources

Web Server Control Syntax