Search code examples
c#asp.netrepeateranonymous-typesdatabound

Anonymous type in Repeater DataBound event


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