codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:



Previous Page  Page 1 Page 2 Page 3 Page 4 Page 6 Page 7 Page 8 Page 9 Page 10 Page 11 Page 12 Page 13 Next Page  

<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.

Previous Page  Page 1 Page 2 Page 3 Page 4 Page 6 Page 7 Page 8 Page 9 Page 10 Page 11 Page 12 Page 13 Next Page  




Click here to Buy!

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.











Recent Forum Threads
•  How do you create....
•  Re: Import Excel File to sql server database...
•  automation of Word Mail Merge
•  Re: Filepointer in Perl
•  Re: dynamic crystal report generation
•  Data transfer from DB2 to Sybase using Perl?
•  is there other ways to get data out from excel using ASP.NET
•  Re: Array manipulations
•  ASP+Bar Code


Recent Articles
What is a pointer in C?
Multiple submit buttons with form validation
Understanding Hibernate ORM for Java/J2EE
HTTP screen-scraping and caching
a javascript calculator
A simple way to JTable
Java Native Interface (JNI)
Parsing Dynamic Layouts
MagicGrid
Caching With ASP.Net


© Copyright codetoad.com 2001-2006