开发者

Eval() and base class properties

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 wit开发者_如何学JAVAh the name 'Id'.

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


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜