我有一个Repeater控件绑定到Linq查询的结果.
我想获得ItemDataBound事件中某个数据源字段的值,但我不知道如何做到这一点.
取决于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(); } }
你可以使用:e.Item.DataItem
.
示例: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= "***Good***"; } } }
可以从EventArgs中找到用于当前项的数据.
RepeaterItemEventArgs e e.Item.DataItem