asp.net.ph

Label Class

System.Web.UI.WebControls Namespace


Renders a label control for displaying text on a Web page.

Label Class Members

Collapse   Constructors

Visibility Constructor Parameters
public Label ( )

Collapse   Properties

Visibility Name Value Type Accessibility
public AssociatedControlID String [ Get , Set ]
public SupportsDisabledAttribute Boolean [ Get ]
public Text String [ Get , Set ]

Collapse   Methods

Visibility Name Parameters Return Type
protected AddAttributesToRender ( HtmlTextWriter writer ) Void
protected AddParsedSubObject ( Object obj ) Void
protected LoadViewState ( Object savedState ) Void

Remarks

Use the Label control to display text in a set location on the page. Unlike static text, the Text property of a label may be set programmatically, enabling you to customize the displayed text at run time.

NOTE: You can also use the Literal and PlaceHolder controls to display text on the Web Forms page. However, unlike the Label control, these controls can not render any additional elements, such as other HTML tags.

Label is particularly useful for programmatically setting text at a specified location on the page at run time, in which the text is determined dynamically, either from user input or from some other process in your code. A typical application is to display the current system date and time on a Web Form.

In certain cases, as when labels are used within templated controls, the label's Text, or a part thereof, is usually obtained dynamically from a data source.

Example

Below is some code snippet from our sample storefront, which displays variable labels based on product specs. Each of the labels is bound to a field from a data source, the values of which are derived dynamically for each item in the DataList at run time.

<itemtemplate>
   <table cellpadding=4 width=100%>
   <tr valign = "top">
      <td width=60 align = "center">
         <asp:hyperlink id = "asinImgLink" runat = "server">
            <img id = "asinImg" runat = "server" border=0></asp:hyperlink>
      </td>
      <td>
         <asp:hyperlink id = "asinUrl" runat = "server" />
         <br>
         <asp:label id = "mfg" runat = "server" />
         <asp:label id = "seller" runat = "server" />
         <asp:label id = "release" runat = "server" />
         <asp:label id = "listprice" runat = "server" />
         <asp:label id = "ourprice" runat = "server" />
         <br>
      </td></tr>
   </table>
</itemtemplate>

 Show me 

While you can set the databinding for <asp:Label> controls at design time, in the case above, databinding is done via the ItemDataBound event handler of the DataList. Below shows a snippet of that code.

void setDataBinding ( Object src, DataListItemEventArgs e ) {
   // handles the onItemDataBound event of the items datalist
   if ( e.Item.ItemType == ListItemType.Item ||
         e.Item.ItemType == ListItemType.AlternatingItem ) {

      // set reference to current data record
      DataRowView rowView = ( DataRowView ) e.Item.DataItem;
      ...

      if ( isInData ( "Manufacturer" ) ) {
         Label label = ( Label ) e.Item.FindControl ( "mfg" );
         label.Text = rowView [ "Manufacturer" ].ToString ( ) + "<br>";
      }

      // for merchants other than amazon
      if ( isInData ( "MerchantId" ) ) {
         // get and set current item's seller
         Label label = ( Label ) e.Item.FindControl ( "seller" );
         label.Text = "<i>" + ( string ) shopDb.fetchScalar
           ( "SELECT storename FROM aspx_shop_stores where merchantid='" +
               rowView [ "MerchantId" ] + "'" ) + "</i></br>";
      }

      // for books, music and videos
      if ( isInData ( "ReleaseDate" ) && ( mode == "books" || mode == "classical" ||
            mode == "dvd" || mode == "music" || mode == "vhs" ) ) {
         Label label = ( Label ) e.Item.FindControl ( "release" );
         label.Text = "<b>Release:</b> " + rowView [ "ReleaseDate" ].ToString ( ) + "<br>";
      }

      // get and set prices
      if ( isInData ( "OurPrice" ) && rowView [ "OurPrice" ].ToString ( ) != "" ) {
         string ourprice = rowView [ "OurPrice" ].ToString ( );
         Label label = ( Label ) e.Item.FindControl ( "ourprice" );
         label.Text = "<b>Our Price:</b> <b style='color:maroon'>" + ourprice + "</b>";
         if ( isInData ( "ListPrice" ) ) {
            string listprice = rowView [ "ListPrice" ].ToString ( );
            if ( string.Compare ( listprice, ourprice ) > 0 ) {
               label = ( Label ) e.Item.FindControl ( "listprice" );
               label.Text = "<b>List Price:</b> <s>" + listprice + "</s><br>";
            }
         }
      }
      ...
   }
}

For other examples illustrating use of this control, see the individual member types of this class. For syntax information, see Label in ASP.NET Syntax for Web Controls.

See Also

Literal   PlaceHolder Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph