Freigeben über


Label Web Server Control

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

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

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   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 use a Label control to display the coordinates of the mouse pointer when the pointer is clicked on the image.

<%@ Page Language="VB" AutoEventWireup="True" %>
<html>
<head>
   <script runat="server">
      Sub ImageButton_Click(sender As Object, e As ImageClickEventArgs) 
         Label1.Text="You clicked the ImageButton control at the " & _
                     "Coordinates: (" & e.X.ToString() & ", " & _
                     e.Y.ToString() & ")"
      End Sub
   </script>
</head>
<body>
   <form runat="server">
      <h3>Label Sample</h3>
      Click anywhere on the image.<br><br>
      <asp:ImageButton id="imagebutton1" runat="server"
           AlternateText="ImageButton 1"
           ImageAlign="left"
           ImageUrl="images\pict.jpg"
           OnClick="ImageButton_Click"/>
      <br><br>
      <asp:label id="Label1" runat="server"/>
   </form>
</body>
</html>
[C#]
<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
<head>
   <script runat="server">
      void ImageButton_Click(object Source, ImageClickEventArgs e) 
      {
         Label1.Text="You clicked the ImageButton control at the " +
                     "Coordinates: (" + e.X.ToString() + ", " +
                     e.Y.ToString() + ")";
      }
   </script>
</head>
<body>
   <form runat="server">
      <h3>Label Sample</h3>
      Click anywhere on the image.<br><br>
      <asp:ImageButton id="imagebutton1"
           AlternateText="ImageButton 1"
           ImageAlign="left"
           ImageUrl="images\pict.jpg"
           OnClick="ImageButton_Click"
           runat="server"/>
      <br><br>
      <asp:Label id="Label1" 
           runat="server"/>
   </form>
</body>
</html>

See Also

Web Server Controls | Label Class