Export (0) Print
Expand All

Adding Label Web Server Controls to a Web Forms Page

You can add Label Web server controls as self-standing controls on the page. You can also make them child controls of other controls, such as using them in Web server control templates.

Note   If you want to display static text, you can present it using HTML; you do not need a Label control. Use a Label control only if you need to change the contents or other characteristics of the text in server code. If you are in linear layout mode, you can simply type text in. If you are in grid layout mode, you can add a Linear Layout Panel element and add static text inside of it.

To add a Label Web server control to a Web Forms page

  1. In Design view, drag a Label control from the Web Forms tab of the Toolbox onto the page.
  2. Set the control's Text property to the text to display. You can include HTML formatting in the property; for example, you can bold an individual word in the text by placing a <B> element around it in the Text property.

    The following example shows how you can set the text of a Label control at run time. The method handles a Button control's Click event and displays in the Label control whatever the user has typed into a TextBox control called TextBox1.

    Security Note   User input in a Web Forms page can include potentially malicious client script. By default, the Web Forms page validates that user input does not include script or HTML elements. For more information, see Scripting Exploits and Protecting Against Script Exploits in a Web Application.
    ' Visual Basic
    Private Sub Button1_Click(ByVal sender as Object, ByVal e as EventArgs)
       Label1.Text = TextBox1.Text
    End Sub
    
    // C#
    private void Button1_Click(object sender, System.EventArgs e) {
       Label1.Text = TextBox1.Text;
    }
    

See Also

Introduction to the Label Web Server Control | Data Binding Single-Value Web Server Controls at Design Time | Setting Web Server Control Properties Programmatically | Customizing the Appearance of ASP.NET Server Controls Using Styles | Label Class

Show:
© 2014 Microsoft