views:

26

answers:

3

Hi

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

A: 

Have you tried simply

<asp:Repeater ID="Repeater1" runat="server"> 
  <ItemTemplate> 
    <%# Eval("FileName") %>
  </ItemTemplate> 
</asp:Repeater> 
marcind
Thanks, but gets the same error.. Am using VS2010 Website Project against .NET3.5
Dave
The others are right. This has to be a property.
marcind
+1  A: 

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!

Scott Mitchell
Dangit! You beat me to it. :) +1
djacobson
Thats it - cheers Scott, and djacobson too :-)
Dave
A: 

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+...)

djacobson