Export (0) Print
Expand All
3 out of 6 rated this helpful - Rate this topic

Repeater.ItemDataBound Event

Occurs after an item in the Repeater control is data-bound but before it is rendered on the page.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)
public event RepeaterItemEventHandler ItemDataBound
<asp:Repeater OnItemDataBound="RepeaterItemEventHandler" />

This event is raised when an item in the Repeater control is data-bound.

For more information about handling events, see Consuming Events.

The following example demonstrates how to specify and code a handler for the ItemDataBound event of the Repeater control. The data is modified after it is bound to an item in the Repeater control but before it is rendered on the page.


<%@ Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
 <head>
    <title>OnItemDataBound Example</title>
<script language="C#" runat="server">
       void Page_Load(Object Sender, EventArgs e) {

          if (!IsPostBack) {
             ArrayList values = new ArrayList();

             values.Add(new Evaluation("Razor Wiper Blades", "Good"));
             values.Add(new Evaluation("Shoe-So-Soft Softening Polish", "Poor"));
             values.Add(new Evaluation("DynaSmile Dental Fixative", "Fair"));

             Repeater1.DataSource = values;
             Repeater1.DataBind();
          }
       }

       void R1_ItemDataBound(Object Sender, RepeaterItemEventArgs e) {

          // This event is raised for the header, the footer, separators, and items.

          // Execute the following logic for Items and Alternating Items.
          if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {

             if (((Evaluation)e.Item.DataItem).Rating == "Good") {
                ((Label)e.Item.FindControl("RatingLabel")).Text= "<b>***Good***</b>";
             }
          }
       }    

       public class Evaluation {

          private string productid;
          private string rating;

          public Evaluation(string productid, string rating) {
             this.productid = productid;
             this.rating = rating;
          }

          public string ProductID {
             get {
                return productid;
             }
          }

          public string Rating {
             get {
                return rating;
             }
          }
       }

    </script>

 </head>
 <body>

    <h3>OnItemDataBound Example</h3>

    <form id="form1" runat="server">

       <br />
       <asp:Repeater id="Repeater1" OnItemDataBound="R1_ItemDataBound" runat="server">
          <HeaderTemplate>
             <table border="1">
                <tr>
                   <td><b>Product</b></td>
                   <td><b>Consumer Rating</b></td>
                </tr>
          </HeaderTemplate>

          <ItemTemplate>
             <tr>
                <td> <asp:Label Text='<%# DataBinder.Eval(Container.DataItem, "ProductID") %>' Runat="server"/> </td>
                <td> <asp:Label id="RatingLabel" Text='<%# DataBinder.Eval(Container.DataItem, "Rating") %>' Runat="server"/> </td>
             </tr>
          </ItemTemplate>

          <FooterTemplate>
             </table>
          </FooterTemplate>

       </asp:Repeater>
       <br />

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


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.