asp.netrepeateritemdatabound

How to access datasource fields in an ASP.NET Repeaters ItemDataBound event?


I have a Repeater control that is being bound to the result of a Linq query.

I want to get the value of one of the datasource's fields in the ItemDataBound event, but I'm not sure how to do this.


Solution

  • You can use: e.Item.DataItem.

    Example: Repeater.ItemDataBound Event

    // This event is raised for the header, the footer, separators, and items.
    void R1_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
    {
      // 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>";
        }
      }
    }