views:

492

answers:

2

Hi All

I'm setting the DataSource of an ASP.NET repeater as follows:

rptTargets.DataSource = from t in DB.SalesTargets select new { t.Target, t.SalesRep.RepName };

Now, in the repeater's OnDataBound event, how can I retrieve the RepName and Target properties from the anonymous type contained in e.Item.DataItem?

Many Thanks

+5  A: 

You can use DataBinder.Eval:

string repName = (string)DataBinder.Eval(e.Item.DataItem, "RepName");
string target = (string)DataBinder.Eval(e.Item.DataItem, "Target");
Richard Szalay
Perfect, thanks Richard.
Ravish
+2  A: 

I know this question has been answered over a year ago, but I've just found a .NET 4.0 solution for this problem.

When you bind your anonymous type to a repeater, you can access the properties in the OnDataBound event like this:

dynamic targetInfo = e.Item.DataItem as dynamic;

string repName = targetInfo.RepName;
string target = targetInfo.Target;
Kristof Claes