How To Use the ItemDataBound Event of Web Forms List Controls in Visual Basic .NET

Article translations Article translations
Article ID: 317429 - View products that this article applies to.
This article was previously published under Q317429
Expand all | Collapse all

On This Page

SUMMARY

This step-by-step article describes how to use the ItemDataBound event of the Web Forms DataGrid, DataList, and Repeater controls to perform special processing on individual rows of data that are displayed in a data-bound list control.

Description of the Technique

When you display a list of items in a data-bound list control, you may want to apply conditional logic to display different information, or to apply different formatting, to different items. You can use the ItemDataBound event to apply this logic as each individual row of data is processed and displayed.

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
  • Microsoft Visual Studio .NET installed on a compatible Microsoft Windows operating system
  • An available instance of Microsoft Internet Information Services (IIS) 5.0 or later with Microsoft .NET Framework installed
This article assumes that you are familiar with the following topics:
  • Microsoft Visual Basic .NET
  • ASP.NET web forms
  • ADO.NET data access

Design the Web Form

  1. Start Visual Studio .NET, and then create a new Visual Basic ASP.NET Web application.
  2. Change the pageLayout property of the Web Form to FlowLayout.
  3. Drag a Repeater control from the toolbox to the form.
  4. Switch from Design view to HTML view.
  5. Between the opening and closing <asp:Repeater> and </asp:Repeater> tags, insert the following code to display Repeater items in a bulleted list:
      <HeaderTemplate><ul></HeaderTemplate>
      <ItemTemplate>
        <li> 
          <asp:Label ID="lblContactName" runat=server ForeColor=Blue
            text='<%# Container.DataItem("ContactName")%>'>
          </asp:Label>
        </li>
      </ItemTemplate>
      <FooterTemplate></ul></FooterTemplate>
    					

Load the Data

  1. Switch to the "code behind" module (WebForm1.aspx.vb).
  2. Add the following statements at the top of the code module:
    Imports System.Data.SqlClient
    Imports System.Data.Common
    					
  3. Add code in the Page_Load event to retrieve the list of customers from the sample Northwind database into a SqlDataReader object. Bind the Repeater control to the DataReader. Adjust the connection string that is listed in the following code to connect to an available SQL Server:
      Dim cn As SqlConnection = New SqlConnection("Data Source=(local);" & _
          "Initial Catalog=Northwind;User ID=user;Password=password")
      cn.Open()
      Dim cmd As SqlCommand = New SqlCommand("SELECT * FROM Customers", cn)
      Dim dr As SqlDataReader = cmd.ExecuteReader()
      Repeater1.DataSource = dr
      Repeater1.DataBind()
    					
  4. Run the project. A bulleted list of contact names from the Northwind Customers list is displayed on the page. Each contact name is formatted in blue by default.

Handle the ItemDataBound Event

  1. Switch back to HTML view for the Web Form. In the opening <asp:Repeater> tag, add the following attribute to set an event handler for the ItemDataBound event:
    OnItemDataBound="FormatRepeaterRow"
    					
  2. Switch back to Code view, and then add the following event handler procedure:
    Protected Sub FormatRepeaterRow(ByVal sender As Object,
        ByVal e As RepeaterItemEventArgs)
      If e.Item.ItemType = ListItemType.Item Or _
          e.Item.ItemType = ListItemType.AlternatingItem Then
        Dim dbr As DbDataRecord = CType(e.Item.DataItem, DbDataRecord)
        If Not IsDBNULL(dbr("Country")) AndAlso dbr("Country") = "USA" Then
          CType(e.Item.FindControl("lblContactName"), Label).ForeColor = _
              System.Drawing.Color.Red
        End If
      End If
    End Sub
    					
  3. Run the project again. Contacts from the United States (13 of the 91 customers) are displayed in red because of the conditional formatting code that is contained in the ItemDataBound event handler.

Troubleshooting

  • If the data source is a DataReader object, you must cast e.Item.DataItem as type DBDataRecord (from System.Data.Common) in the event handler. If the data source is a DataTable, you must cast e.Item.DataItem as type DataRowView.
  • Even if you have not created an <AlternatingItemTemplate> entry in your Repeater control, every other row still has an ItemType value of ListItemType.AlternatingItem. Therefore, you must look for both ListItemType.Item and ListItemType.AlternatingItem to process every row.

REFERENCES

For additional information, click the article number below to view the article in the Microsoft Knowledge Base:
307860 INFO: ASP.NET Data Binding Overview

Properties

Article ID: 317429 - Last Review: May 23, 2005 - Revision: 2.6
APPLIES TO
  • Microsoft Visual Studio .NET 2003 Enterprise Architect
  • Microsoft Visual Studio .NET 2003 Enterprise Developer
  • Microsoft Visual Studio .NET 2003 Academic Edition
  • Microsoft Visual Studio .NET 2002 Professional Edition
  • Microsoft Visual Studio .NET 2002 Enterprise Architect
  • Microsoft Visual Studio .NET 2002 Enterprise Developer
  • Microsoft Visual Studio .NET 2002 Academic Edition
Keywords: 
kbdatabinding kbhowtomaster KB317429

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com