如何访问ASP.NET Repeater ItemDataBound事件中的数据源字段?

时间:2021-02-25 03:35:14

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

我有一个Repeater控件,它绑定到Linq查询的结果。

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.

我想获取ItemDataBound事件中某个数据源字段的值,但我不知道如何做到这一点。

3 个解决方案

#1


27  

You can use: e.Item.DataItem.

您可以使用:e.Item.DataItem。

Example: Repeater.ItemDataBound Event

示例:Repeater.ItemDataBound事件

// 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


38  

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

取决于DataSource ...如果您的DataSource是DataTable,那么您的DataItem包含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


6  

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

可以从EventArgs中找到用于当前项目的数据。

RepeaterItemEventArgs e

e.Item.DataItem

#1


27  

You can use: e.Item.DataItem.

您可以使用:e.Item.DataItem。

Example: Repeater.ItemDataBound Event

示例:Repeater.ItemDataBound事件

// 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


38  

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

取决于DataSource ...如果您的DataSource是DataTable,那么您的DataItem包含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


6  

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

可以从EventArgs中找到用于当前项目的数据。

RepeaterItemEventArgs e

e.Item.DataItem