Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
ListView Class
ListView Events
 ItemDataBound Event
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
ListView..::.ItemDataBound Event

Occurs when a data item is bound to data in a ListView control.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web.Extensions (in System.Web.Extensions.dll)
Visual Basic (Declaration)
Public Event ItemDataBound As EventHandler(Of ListViewItemEventArgs)
Visual Basic (Usage)
Dim instance As ListView
Dim handler As EventHandler(Of ListViewItemEventArgs)

AddHandler instance.ItemDataBound, handler
C#
public event EventHandler<ListViewItemEventArgs> ItemDataBound
Visual C++
public:
 event EventHandler<ListViewItemEventArgs^>^ ItemDataBound {
    void add (EventHandler<ListViewItemEventArgs^>^ value);
    void remove (EventHandler<ListViewItemEventArgs^>^ value);
}
JScript
JScript does not support events.
ASP.NET
<asp:ListView OnItemDataBound="EventHandler" />

Before the ListView control can be rendered, each item in the control must be bound to a record in the data source. The ItemDataBound event is raised when a data item is bound to data in the ListView control. (Data items are represented by ListViewDataItem objects.) This enables you to perform a custom routine whenever this event occurs, such as modifying the values of the data that is bound to the item.

A ListViewItemEventArgs object is passed to the event handler, which enables you to access the properties of the item that is being bound. To access a specific control in the item, use the FindControl method of the ListViewItem object that is contained in the Item property of the ListViewItemEventArgs object. You can determine which item type (data item, insert item, empty item) is being bound by using the ItemType property.

For more information about how to handle events, see Consuming Events.

The following example shows how to use the ItemDataBound event.

Visual Basic
<%@ Page language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

  Protected Sub ContactsListView_ItemDataBound(ByVal sender As Object, _
                                               ByVal e As ListViewItemEventArgs)

    If e.Item.ItemType = ListViewItemType.DataItem Then
      ' Display the e-mail address in italics.
      Dim EmailAddressLabel As Label = _
        CType(e.Item.FindControl("EmailAddressLabel"), Label)
      EmailAddressLabel.Font.Italic = True
    End If

  End Sub

</script>

<html  >
  <head id="Head1" runat="server">
    <title>ListView ItemDataBound Example</title>
  </head>
  <body style="font: 10pt Trebuchet MS">
    <form id="form1" runat="server">

      <h3>ListView ItemDataBound Example</h3>

      <asp:ListView ID="ContactsListView" 
        DataSourceID="ContactsDataSource" 
        ConvertEmptyStringToNull="true"        
        OnItemDataBound="ContactsListView_ItemDataBound"
        runat="server">
        <LayoutTemplate>
          <table cellpadding="2" width="680px" border="0">
            <tr style="background-color: #ADD8E6" runat="server">
                <th runat="server">First Name</th>
                <th runat="server">Last Name</th>
                <th runat="server">E-mail Address</th>
            </tr>
            <tr runat="server" id="itemPlaceholder" />
          </table>
          <asp:DataPager runat="server" ID="PeopleDataPager" PageSize="12">
            <Fields>
              <asp:NumericPagerField ButtonCount="10" /> 
            </Fields>
          </asp:DataPager>
        </LayoutTemplate>
        <ItemTemplate>
          <tr style="background-color: #CAEEFF" runat="server">
            <td>
              <asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("FirstName") %>' />
            </td>
            <td>
              <asp:Label ID="LastNameLabel" runat="server" Text='<%#Eval("LastName") %>' />
            </td>
            <td>
              <asp:Label ID="EmailAddressLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
            </td>
          </tr>
        </ItemTemplate>
      </asp:ListView>

      <!-- This example uses Microsoft SQL Server and connects      -->
      <!-- to the AdventureWorks sample database. Use an ASP.NET    -->
      <!-- expression to retrieve the connection string value       -->
      <!-- from the Web.config file.                                -->
      <asp:SqlDataSource ID="ContactsDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT FirstName, LastName, EmailAddress FROM Person.Contact">
      </asp:SqlDataSource>

    </form>
  </body>
</html>
C#
<%@ Page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

  protected void ContactsListView_ItemDataBound(object sender, ListViewItemEventArgs e)
  {
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
      // Display the e-mail address in italics.
      Label EmailAddressLabel = (Label)e.Item.FindControl("EmailAddressLabel");
      EmailAddressLabel.Font.Italic = true;
    }
  }

</script>

<html  >
  <head id="Head1" runat="server">
    <title>ListView ItemDataBound Example</title>
  </head>
  <body style="font: 10pt Trebuchet MS">
    <form id="form1" runat="server">

      <h3>ListView ItemDataBound Example</h3>

      <asp:ListView ID="ContactsListView" 
        DataSourceID="ContactsDataSource" 
        ConvertEmptyStringToNull="true"        
        OnItemDataBound="ContactsListView_ItemDataBound"
        runat="server">
        <LayoutTemplate>
          <table cellpadding="2" width="680px" border="0">
            <tr style="background-color: #ADD8E6" runat="server">
                <th runat="server">First Name</th>
                <th runat="server">Last Name</th>
                <th runat="server">E-mail Address</th>
            </tr>
            <tr runat="server" id="itemPlaceholder" />
          </table>
          <asp:DataPager runat="server" ID="PeopleDataPager" PageSize="12">
            <Fields>
              <asp:NumericPagerField ButtonCount="10" /> 
            </Fields>
          </asp:DataPager>
        </LayoutTemplate>
        <ItemTemplate>
          <tr style="background-color: #CAEEFF" runat="server">
            <td>
              <asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("FirstName") %>' />
            </td>
            <td>
              <asp:Label ID="LastNameLabel" runat="server" Text='<%#Eval("LastName") %>' />
            </td>
            <td>
              <asp:Label ID="EmailAddressLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
            </td>
          </tr>
        </ItemTemplate>
      </asp:ListView>

      <!-- This example uses Microsoft SQL Server and connects      -->
      <!-- to the AdventureWorks sample database. Use an ASP.NET    -->
      <!-- expression to retrieve the connection string value       -->
      <!-- from the Web.config file.                                -->
      <asp:SqlDataSource ID="ContactsDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT FirstName, LastName, EmailAddress FROM Person.Contact">
      </asp:SqlDataSource>

    </form>
  </body>
</html>

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Bind Control In EditItemTemplate of ListView      Sunny Patil ... Thomas Lee   |   Edit   |   Show History
protected void ListView_Category_ItemDataBound(object sender, ListViewItemEventArgs e)
{
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
if (ListView_Category.EditItem != null)
{
if (dataItem.DisplayIndex == ListView_Category.EditIndex)
{
//Find datalist control in edititemtemplate of listview.

DataList dl_Product = (DataList)e.Item.FindControl("dl_Product");
foreach (DataListItem item in dl_Product.Items)
{
CheckBox chk_Product = (CheckBox)item.FindControl("chk_Product");
chk_Product.Checked = true;
}

//Or find label control in edititemtemplate of listview.

Label lbl_ProductName = (Label)e.Item.FindControl("lbl_ProductName");
lbl_ProductName.Text = "Assign Text";
}
}
}
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2010 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker