Microsoft KB Archive/317582

= HOW TO: Use the ItemDataBound Event of Web Forms List Controls in Visual C# .NET =

Article ID: 317582

Article Last Modified on 9/4/2003

-

APPLIES TO


 * Microsoft Visual Studio .NET 2002 Professional Edition
 * Microsoft Visual Studio .NET 2003 Professional Edition

-



This article was previously published under Q317582



For a Microsoft Visual Basic .NET version of this article, see 317429.

IN THIS TASK
SUMMARY
 * Description of the Technique
 * Requirements
 * Design the Web Form
 * Load the Data
 * Handle the ItemDataBound Event
 * Troubleshooting

REFERENCES



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.

back to the top

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 because each individual row of data is processed and displayed.

back to the top

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 5.0 or later with the Microsoft .NET Framework installed

This article assumes that you are familiar with the following topics:
 * The Microsoft Visual C# .NET language
 * Microsoft ASP.NET Web forms
 * ADO.NET data access

back to the top

Design the Web Form
 Start Visual Studio .NET, and then create a new Microsoft Visual C# ASP.NET Web application. Change the pageLayout property of the Web Form to FlowLayout. Drag a Repeater control from the Web Form toolbox to the form. Switch from Design view to HTML view by right-clicking the Design window, and then clicking View HTML Source.  Insert the following text between the opening and closing  and  tags to display Repeater items in a bulleted list: <ul></HeaderTemplate> <ItemTemplate>  <asp:Label ID=&quot;lblContactName&quot; runat=server ForeColor=Blue text='<%# DataBinder.Eval(Container.DataItem, &quot;ContactName&quot;)%>'> </asp:Label> </li> </ItemTemplate> <FooterTemplate></ul></FooterTemplate> </li></ol>

back to the top

Load the Data
<ol> Switch to the &quot;code behind&quot; module (WebForm1.aspx.cs) by right-clicking WebForm1.aspx in Solution Explorer, and then clicking View Code.</li>  Add the following statements at the top of the code module: using System.Data.SqlClient; using System.Data.Common; </li>  Add code in the Page_Load event to retrieve the list of customers from the sample Northwind database to a SqlDataReader object, and then bind the Repeater control to the DataReader. Modify the connection string as necessary in the code that follows to connect to an available SQL Server: SqlConnection cn = new SqlConnection(&quot;Data Source=(local);Initial   Catalog=Northwind;User ID=user;Password=password&quot;); cn.Open; SqlCommand cmd = new SqlCommand(&quot;SELECT * FROM Customers&quot;, cn); SqlDataReader dr = cmd.ExecuteReader; Repeater1.DataSource = dr; Repeater1.DataBind; </li> 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.</li></ol>

back to the top

Handle the ItemDataBound Event
<ol>  Switch back to HTML view for the Web Form. Add the following attribute in the opening  tag to specify an event handler for the ItemDataBound event: OnItemDataBound=&quot;FormatRepeaterRow&quot; </li>  Switch back to Code view, and then add the following event handler procedure: protected void FormatRepeaterRow(Object sender, RepeaterItemEventArgs e) { if( (e.Item.ItemType == ListItemType.Item) || ( e.Item.ItemType == ListItemType.AlternatingItem)) {    DbDataRecord dbr = (DbDataRecord)e.Item.DataItem; if( Convert.ToString(DataBinder.Eval(dbr, &quot;Country&quot;)) == &quot;USA&quot; ) ((Label)e.Item.FindControl(&quot;lblContactName&quot;)).ForeColor = System.Drawing.Color.Red; }         }                    </li> Run the project again. Contacts from the United States (typically 13 of the 91 customers) are displayed in red because of the conditional formatting code in the ItemDataBound event handler.</li></ol>

back to the top

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 object, you must cast e.Item.DataItem as type DataRowView.
 * Even if you have not created an <AlternatingItemTemplate> in your Repeater control, every other row still has an ItemType of ListItemType.AlternatingItem. Therefore, you must check for both ListItemType.Item and ListItemType.AlternatingItem to process every row.

back to the top

<div class="references_section">