Compartilhar via


Label Web Server Control Overview

The Label Web server control lets you programmatically set text in an ASP.NET Web page.

This topic contains:

  • Scenarios

  • Features

  • Background

  • Code Examples

  • Class Reference

Scenarios

You typically use the Label control when you want to change text in the page at run time, such as in response to a button click.

Back to top

Features

You can set the text of the Label control at design time or at run time in a program. You can also bind the Label control's Text property to a data source to display database information on a page.

Back to top

Background

You can use the Label control to act as an active caption in front of a TextBox control or other control. Users can then press an access key to move to the control for which the Label control is the caption. For details, see How to: Use Label Web Server Controls as Captions.

The Label control provides one way to display text in an ASP.NET Web page. Other options include the following:

  • HTML markup   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.

  • TheLiteral control   Like the Label control, the Literal control lets you programmatically display text in the page. However, the Literal control does not support style properties and does not support themes or skins.

Using Labels in List Controls

The Label control is often used in list Web server controls (such as the Repeater, DataList, GridView, DetailsView, and FormView controls) to display read-only information from a database. The usual strategy is to add a Label control to a template for one of these controls. You then bind the Label control to a data source. For details about templates, see ASP.NET Web Server Controls Templates.

Security and the Label Control

You can set the Text property of the Label control to any string, including strings that contain markup. If the string contains markup, the Label control interprets the markup. For example, if you set the Text property to <b>Test</b>, the Label control renders the word Test in bold type.

To avoid security issues such as the possibility of script injection, do not set the Text property to a string that contains markup that comes from an untrusted source. If you do not trust the source of the string that you are using for the Text property, you should encode the string. For details, see How to: Protect Against Script Exploits in a Web Application by Applying HTML Encoding to Strings.

Back to top

Code Examples

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

How to: Use Label Web Server Controls as Captions

Back to top

Class Reference

The following table lists the classes that relate to the Label control.

Member

Description

Label

The main class for the control.

Back to top

See Also

Reference

Literal Web Server Control Overview