|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.|
Label Web Server Control Overview
TheWeb server control provides you with a way to programmatically set text in an ASP.NET Web page. 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.
The Label control provides one way to display text in an ASP.NET Web page. Other options include the following:
HTML 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.
Thecontrol Like the Label control, the Literal control allows you to programmatically set text to be displayed in the page. However, the Literal control does not support style properties and does not support themes or skins.
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'sproperty to a data source to display database information on a page.
The Label control is often used in list Web server controls (, , , , and 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 .
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 will render 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 containing 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.