JavaScript Editor jscript editor     Web designer 



Main Page

You can customize items in the DataList control dynamically, which is useful if you want to set item characteristics based on information available at run time. This topic contains two examples that show how to customize the items in a DataList control.

The first code example shows how to customize an individual item when the customization does not involve data. The example displays different types of templates with different background colors.

The second code example shows how to customize the contents of a DataList control item based on the data being displayed in it. The code examines the contents of a data-bound label in the item to extract a date (date of birth). If the date-of-birth month is the same as the current month, the item is highlighted using color.

Example

Visual BasicВ CopyCode imageCopy Code
' Example 1
Private Sub DataList1_ItemCreated(ByVal sender As Object, _
        ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) _
        Handles DataList1.ItemCreated
    Select Case e.Item.ItemType
        Case ListItemType.Item
            e.Item.BackColor = Color.LightGoldenrodYellow
        Case ListItemType.AlternatingItem
            e.Item.BackColor = Color.DarkGoldenrod
        Case ListItemType.SelectedItem
            e.Item.BackColor = Color.Yellow
        Case Else
         ' Add code here to handle the header, footer, and
         ' separator templates.
    End Select
End Sub

' Example 2
Private Sub DataList1_ItemDataBound(ByVal sender As Object, _
        ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) _
        Handles DataList1.ItemDataBound
    Dim dob As DateTime
    Dim doblabel As Label
    doblabel = CType(e.Item.FindControl("Label1"), Label)
    dob = CType(doblabel.Text, DateTime)
    If dob.Month = Today.Month Then
        e.Item.BackColor = Color.Yellow
    End If
End Sub

Compiling the Code

These examples require:

  • A DataList control named DataList1 that is bound to the Employees table of the Northwind database. For information on binding a DataList control to a data source, see How to: Add DataList Web Server Controls to an ASP.NET Web Page (Visual Studio).

  • An item template containing at least one Label control called Label1. This label should be bound to the BirthDate field of the Employees table. For information on binding controls to fields, see Data-Binding Expression Syntax.

Robust Programming

The most reliable way to extract the value of a control in a DataList control template is to:

  1. Assign an ID to the control at design time.

  2. Use the FindControl method of the naming container (in this example, a DataListItem object).

The FindControl method returns an object of type Control. You must cast this object to the appropriate control type. If no object is found and you try to extract its value, the control throws an exception of type NullReferenceException.

See Also



JavaScript Editor jscript editor     Web designer