Share via


How to: Add 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, for example by using them in ASP.NET Web Server Controls Templates.

NoteNote

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.

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

  1. Type an <asp:Label> element into the page. For syntax, see Label Web Server Control.

  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 code example shows how you can set the text of a Label control at run time. The method displays in the Label control whatever the user has typed into a TextBox control called TextBox1.

    Security noteSecurity Note

    Be careful when using strings that come from an untrusted source; they can include potentially malicious client script. For details, see How to: Protect Against Script Exploits in a Web Application by Applying HTML Encoding to Strings.

    Private Sub Button1_Click(ByVal sender as Object, _
           ByVal e as EventArgs)
       Label1.Text = Server.HtmlEncode(TextBox1.Text)
    End Sub
    
    private void Button1_Click(object sender, System.EventArgs e) {
       Label1.Text = Server.HtmlEncode(TextBox1.Text);
    }
    

See Also

Reference

Label Web Server Control Overview