views:

17

answers:

1

I have class Foo which defines property Id. Class Bar inherits from Foo (class Bar : Foo).

If I assign a List<Bar> to Repeater.DataSource then use Eval("Id") in the ItemTemplate, the following exception is thrown:

DataBinding: 'Bar' does not contain a property with the name 'Id'.

Any way around this? Id is a valid property of Bar, it's just defined on Foo.

+1  A: 

It works fine for me. Maybe you have a visibility problem? What is the access modifier on the Id property?

Here is my source:

public partial class WebForm1 : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {
      List<Bar> bars = new List<Bar>();
      bars.Add(new Bar());
      bars.Add(new Bar());
      bars.Add(new Bar());

      Repeater1.DataSource = bars;
      Repeater1.DataBind();
   }
}

public class Foo
{
   public Foo()
   {
      this.FooProp = "FooPropValue";
   }

   public string FooProp { get; set; }
}

public class Bar : Foo
{
   public Bar()
   {
      this.BarProp = "BarPropValue!";
   }

   public string BarProp { get; set; }
}

And in the ASPX I have:

 <asp:Repeater ID="Repeater1" runat="server">
 <ItemTemplate><%# Eval("FooProp")%></ItemTemplate>
 </asp:Repeater>
Russell McClure
Great, I feel like an idiot. In my defense it was the end of the week. :)
Nelson