views:

2014

answers:

3

Hi

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.

Regards

Peter

+3  A: 

e.Item.DataItem

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemdatabound.aspx

 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>";
             }
          }
       }
Brian Hedler
A: 

The data that is used for the current item can be found from the EventArgs.

RepeaterItemEventArgs e

e.Item.DataItem
Robin Day
+1  A: 

Depending on the DataSource... If your DataSource is a DataTable, then your DataItem contains a DataRowView :

    protected void rptMyReteater_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Button b = e.Item.FindControl("myButton") as Button; 
            DataRowView drv = e.Item.DataItem as DataRowView;
            b.CommandArgument = drv.Row["ID_COLUMN_NAME"].ToString();
        }
    }
Sprotch