Tutorial :C# Generic List and ASP Repeater Fun



Question:

I'm trying to render out some images on an aspx page.

Error I'm getting in the code below is: DataBinding: '_Default+ImageThing' does not contain a property with the name 'FileName'.

    public class ImageThing      {          public string FileName;      }        private void DisplayThumbnailImages()      {          ImageThing imageThing1 = new ImageThing();          ImageThing imageThing2 = new ImageThing();          imageThing1.FileName = "asdf.jpg";          imageThing2.FileName = "aaa.jpg";            List<ImageThing> imagesToRender = new List<ImageThing>();          imagesToRender.Add(imageThing1);          imagesToRender.Add(imageThing2);            Repeater1.DataSource = imagesToRender;          Repeater1.DataBind();      }  

here is the aspx:

       <asp:Repeater ID="Repeater1" runat="server">              <ItemTemplate>              <%#DataBinder.Eval(Container.DataItem, "FileName")%>              </ItemTemplate>          </asp:Repeater>  

Thanks


Solution:1

The data binding syntax does not work with fields, it only works with properties. Try making this change to your ImageThing class:

public class ImageThing  {      public string FileName { get; set; }  }  

Now it's a property and now you should be able to access is via the template using <%#DataBinder.Eval(Container.DataItem, "FileName")%> (or even better, just <%# Eval("FileName") %>).

Happy Programming!


Solution:2

Have you tried simply

<asp:Repeater ID="Repeater1" runat="server">     <ItemTemplate>       <%# Eval("FileName") %>    </ItemTemplate>   </asp:Repeater>   


Solution:3

DataBinder.Eval is (I assume) using reflection to access the FileName property of your ImageThing. Try making FileName an actual property, not just a public field, like so: public string FileName { get; set; }, does it work?

(Note that the auto-implementing property syntax used above is only available in C# 3.0+...)


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »