Web Control: The Label
A label is text that is presented to the visitor. To create such text, you can just click anywhere on the web page and type the desired string. If you…
ASP.NET - Lesson 01:
Web Control: The Label#
Labels Fundamentals#
Introduction#
Labels are used to show text to website visitors. While you can simply type plain text anywhere on a page, using a Label control—which is part of the Label class—allows the text to be dynamic and interactive.
Creating a Label#
To visually create a label from either Microsoft Visual Studio or Microsoft Visual Web Developer, from the Standard section of the Toolbox, drag the Label object and drop it on the form. To manually add a label, create an <asp:Label> tag and add it to the form. Here is an example:
<%@ Page Language="C#" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<form id="frmTimeSheet" method="post" runat="server">
<asp:Label ID="btnFullName" runat="server"></asp:Label>
</form>
</body>
</html>Characteristics of a Label#
The Caption of a Label#
Obviously the most significant aspect of a label is the text it displays. To specify this text, add a Text attribute to the tag and assign the desired string to it. Here is an example:
<%@ Page Language="C#" %>
<html>
<head>
<script language="JavaScript" type="text/javascript">
</script>
<title>Exercise</title>
</head>
<body>
<form id="frmTimeSheet" method="post" runat="server">
<asp:Label ID="btnFullName" Text="Full Name:" runat="server"></asp:Label>
</form>
</body>
</html>This would produce:
The primary benefit of a Label over static text is the ability to modify its content via code based on user interactions. You can achieve this by accessing the label's ID, using the Text property, and assigning a new string value. Home Copyright © 2009 C# Key

