Jaa


How to: Add Label Web Server Controls to a Web Forms Page

You can add Label Web server controls as self-standing controls on your ASP.NET Web page. You can also make Label controls child controls of other controls, such as using them in Web server control templates. For details, see ASP.NET Web Server Controls Templates.

Note

If you want to display static text, you can present it by using HTML — you do not need a Label control. Use a Label control only when you need to change the contents or appearance of text programmatically.

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

  1. From the Standard tab of the Toolbox, drag a Label control onto the page.

  2. In the Appearance category of the Properties window, set the control's Text property to the text to display. You can include HTML formatting in the property; for example, you can format an individual word in the text as bold by placing a <b> tag 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 displays in the Label control whatever the user has typed into the TextBox control named TextBox1.

    Security noteSecurity Note:

    Be careful when using the Label control to display 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.

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

See Also

Reference

Label Web Server Control Overview