asp.net.ph

Label Constructor

System.Web.UI.WebControls Namespace   Label Class


Initializes a new instance of the Label class.

[ VB ]
Public Sub New ( )

[ C# ]
public Label ( );

[ C++ ]
public: Label ( );

[ JScript ]
public function Label ( );

Remarks

Use this constructor to programmatically initialize a new instance of the Label control at run time.

This method is useful when there is need to conditionally add or insert a new Label control at some specified location on the page at run time, particularly when the number of Label controls to generate are unknown at design time. Typical applications include programmatically adding Label controls to dynamically generated list controls, such as the Repeater, DataList or DataGrid.

Example

Below is some code snippet from our sample search results page that illustrates, among others, how to programmatically add Label controls to the ItemTemplate of a DataList control that is generated dynamically at run time. How many labels to render depends on how many records are returned in the results set.

void buildItemTemplate ( Control ctrl ) {

   Table table; TableRow tr; TableCell td;

   // setup search results
   table = new Table ( );

   // add a row for each item
   tr = new TableRow ( );

   // display search results item
   td = new TableCell ( );
   td.Controls.Add ( getManufacturer ( ) );
   td.Controls.Add ( getListPrice ( ) );
   td.Controls.Add ( getOurPrice ( ) );
   tr.Controls.Add ( td );

   table.Controls.Add ( tr );

   IParserAccessor parser = ( ( IParserAccessor ) ( ctrl ) );
   parser.AddParsedSubObject ( table );
}

private Control getManufacturer ( ) {
    Label ctrl = new Label ( );
    ctrl.ID = "mfg";
    return ctrl;
}

private Control getListPrice ( ) {
    Label ctrl = new Label ( );
    ctrl.ID = "listprice";
    return ctrl;
}

private Control getOurPrice ( ) {
    Label ctrl = new Label ( );
    ctrl.ID = "ourprice";
    return ctrl;
}

 Show me 

Akin to <asp:Label> controls declared at design time, the Text of dynamically generated labels often is derived from a data source. In the case above, databinding is done via the ItemDataBound event handler of the DataList. Below shows a snippet of that code.

void setBinding ( Object src, DataListItemEventArgs e ) {
   // handles the onItemDataBound event of the search results 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>";
      }

      // 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>";
            }
         }
      }
      ...
   }
}
See Also

Label Members 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