<asp:label> Control Examples
To create the control with the minimum of information needed, you can just supply the runat and id attributes:
 <asp:label id="Message1"
runat="server">Hello</asp:label>
Placed in the context of a web page, the
control might look like this:
<html>
<head>
 <title>Another Hello World Page </title>
</head>
<body>
 Hello
 <asp:label id="Message1"
runat="server"> World</asp:label>
</body>
</html>
The id attribute is used to uniquely identify the <asp:label> control so you can refer to it in your ASP.NET code. The runat="server" attribute tells the server to process the control and generate HTML
code to be sent to the client.
Let's look at
another example. If you want to set the color of a text message to red, you
could set it like this:
<asp:label
id="Message1" forecolor="red"
runat="server">Hello</asp:label>
Alternatively, you can use the text attribute. This way, everything
can be contained within the opening tag, in which case you need to close the
tag in the following way:
<asp:label
id="Message1" forecolor="red"Â text="Hello" runat="server" />
Here, we omit the closing tag, and just
supply a closing / to indicate that
the tag is closed. Throughout the book, if the tag doesn't enclose any content,
then we will use this latter notation in preference to having a closing tag.
The <asp:> prefix indicates that this control is part of the set of built-in
ASP.NET controls. It is possible to create custom controls, which have prefixes
of the developer's choice. We will look at this later in the book.
Let's now take a look at an example of how
we can use the <asp:label>
control to display some text at requisite places within our web page. In this
example, we'll assume that values of the user's name and the destination they
selected on our holiday web site have already been passed across, and that all
we need to do is output a message displaying confirmation that we have received
the user's details.
Try It Out – Using the <asp: label> Control
1. Enter the following code in your web page editor:
<html>
<head>
 <title>Label Control page</title>
</head>
<body>
 <h1>Feiertag Holidays</h1>
 <br /><br />
 Thank you
 <asp:label id="Message1" runat="server"
text="Chris"/>
   you have selected to receive information about
 <asp:label id="Message2" runat="server"
text="Oslo"/>
.  The information pack will be sent out within the next 24 hours.
</body>
</html>
2. Save this as labelcontrol.aspx, in your test directory, and view it from your browser:
How It Works
There isn't much to explain here. Our <asp:label> controls fit neatly within the HTML code:
<h1>Feiertag
Holidays</h1>
<br /><br />
Thank you
<asp:label
id="Message1" runat="server" text="Chris"/>
 you have selected to receive information about
<asp:label
id="Message2" runat="server" text="Oslo"/>
 .The information pack will be sent out within the next 24 hours.
</body>
For all intents and purposes the <asp:label> control could be an HTML control. The only thing that
differentiates it is the fact that it is executed on the server. The only way
you can note this is by checking the underlying HTML source code that is sent
back to the browser:
<h1>Feiertag
Holidays</h1>
<br /><br />
Thank you
<span
id="Message1">Chris</span>
 you have selected to receive information about
<span
id="Message2">Oslo</span>
 .The information pack will be sent out within the next 24 hours.
</body>
The <asp:label> controls are translated into HTML <span> tags, to which they are functionally equivalent.
Buy Beginning ASP.NET with C# here
© Copyright 2002 Wrox Press
This chapter is written by David Sussman, et al
and taken from "Beginning ASP.NET with C#" published by Wrox Press Limited in June 2002; ISBN 1861007345; copyright © Wrox Press Limited 2002; all rights reserved.
No part of these chapters may be reproduced, stored in a retrieval system or transmitted in any form or by any means -- electronic, electrostatic, mechanical, photocopying, recording or otherwise -- without the prior written permission of the publisher, except in the case of brief quotations embodied in critical articles or reviews.
|
|