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

大城市里の小女人 提交于 2019-12-20 09:49:36

问题


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.


回答1:


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>";
    }
  }
} 



回答2:


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();
    }
}



回答3:


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

RepeaterItemEventArgs e

e.Item.DataItem


来源:https://stackoverflow.com/questions/829002/how-to-access-datasource-fields-in-an-asp-net-repeaters-itemdatabound-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!