Partilhar via


How to: Customize DataList Items at Run Time

During page processing, the DataList control creates individual items that it will render to the page. To allow you to customize the items as they are being created, the DataList control raises two events:

  • The ItemCreated event is raised as the DataList control creates each item, alternating item, header, footer, and so on.

  • The ItemDataBound event is raised after the controls in an item have been data bound. You can use the event parameters to access the data being used for data binding. Use this event if the customization depends on the data.

By responding to these events, you can change the items. A typical use is to change the appearance of the item based on the data being displayed in it. For example, if a numeric value you read from a database is negative, you might set the item's background color to red.

To customize items at run time using the ItemCreated event

  1. Create an event handler for the DataList control's ItemCreated event.

  2. In the event handler, get the current item using the Item object from the e event-argument object. The index of the current item is available through the Item object's ItemIndex property, and its type is available through its ItemType property (which is defined using the ListItemType enumeration).

    NoteNote

    The ItemIndex property returns a positive value only for Item, AlternatingItem, and SelectedItem objects. The index value for header, footer, and separator items is -1.

    The following example shows how you can modify the background color of an item conditionally. The example uses the ItemType property and the ListItemType enumeration to set the background colors of the items. Items are set to LightGoldenrodYellow, alternating items are set to DarkGoldenrod, and the selected item is set to Yellow.

    Protected Sub DataList1_ItemCreated(ByVal sender As Object, _
            ByVal e As EventArgs)
       Select Case e.Item.ItemType
          Case ListItemType.Item
             e.Item.BackColor = _
                 System.Drawing.Color.LightGoldenrodYellow
          Case ListItemType.AlternatingItem
             e.Item.BackColor = System.Drawing.Color.DarkGoldenrod
          Case ListItemType.SelectedItem
             e.Item.BackColor = System.Drawing.Color.Yellow
          Case Else
             ' Add code here to hand the header, footer, and
             ' separator templates.
       End Select
    End Sub
    
    protected void DataList1_ItemCreated(object sender, 
        EventArgs e)
    {
       switch (e.Item.ItemType)
       {
          case ListItemType.Item :
             e.Item.BackColor = 
                 System.Drawing.Color.LightGoldenrodYellow;
             break;
          case ListItemType.AlternatingItem :
             e.Item.BackColor = System.Drawing.Color.DarkGoldenrod;
             break;
          case ListItemType.SelectedItem :
             e.Item.BackColor = System.Drawing.Color.Yellow;
             break;
          default :
             // Add code here to handle the header, footer, and 
             // separator templates.
             break;
          }
    }
    

At the time that the ItemCreated event is raised, the individual controls in the templates have not yet been bound to data. However, the data is available in the ItemDataBound event.

To customize items at run time using the ItemDataBound event

  1. Create an event handler for the DataList control's ItemDataBound event.

  2. In the event handler, test that you are working with an Item, AlternatingItem, or EditItem and not with a header or footer.

  3. Use the DataItem property to test the data that will be bound to the control and base a comparison on that.

    The following example shows how you can make a conditional test based on data. The example casts the DataItem property of the event argument as a DataRowView object. It then extracts the field called Quantity from the current DataItem. If the quantity is less than 10, the item in the DataList control is colored red.

    Protected Sub DataList1_ItemDataBound(ByVal sender As Object, _
            ByVal e As DataListItemEventArgs)
        If e.Item.ItemType = ListItemType.Item Or _
                e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim drv As System.Data.DataRowView = _
                CType(e.Item.DataItem, System.Data.DataRowView)
            Dim quantity As Integer = _
                Integer.Parse(drv.Row("Quantity").ToString())
            If (quantity < 10) Then
                e.Item.BackColor = System.Drawing.Color.Red
            End If
        End If
    End Sub
    
    protected void DataList1_ItemDataBound(object sender, 
            DataListItemEventArgs e)
    {
       if(e.Item.ItemType == ListItemType.Item || 
              e.Item.ItemType == ListItemType.AlternatingItem)
       {
           System.Data.DataRowView drv = 
               (System.Data.DataRowView)(e.Item.DataItem);
           int quantity = int.Parse(drv.Row["Quantity"].ToString());
           if (quantity < 10)
           {
              e.Item.BackColor = Color.Red;
           }
        }
    }
    

See Also

Reference

DataList Web Server Control Overview