Anonymous type in Repeater DataBound event
-
06-07-2019 - |
Question
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
Solution
You can use DataBinder.Eval:
string repName = (string)DataBinder.Eval(e.Item.DataItem, "RepName");
string target = (string)DataBinder.Eval(e.Item.DataItem, "Target");
OTHER TIPS
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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow