13

I have a list collection like below :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace FileExplorer.Classes
{
    public class NewAddedFiles
    {
        public string FileName;
        public string FilePath;
        public DateTime FileCreationDate;
    }
}

private void GetFilesFromDirectory(string PhysicalPath)
{
    DirectoryInfo Dir = new DirectoryInfo(PhysicalPath);
    FileInfo[] FileList = Dir.GetFiles("*.*", SearchOption.AllDirectories);
    List<NewAddedFiles> list = new List<NewAddedFiles>();
    NewAddedFiles NewAddedFile = new NewAddedFiles();
    foreach (FileInfo FI in FileList)
    {
        //Response.Write(FI.FullName);
        //Response.Write("<br />");
        string AbsoluteFilePath = FI.FullName;
        string RelativeFilePath = "~//" + AbsoluteFilePath.Replace(Request.ServerVariables["APPL_PHYSICAL_PATH"], String.Empty);
        NewAddedFile.FileName = FI.Name;
        NewAddedFile.FilePath = RelativeFilePath;
        NewAddedFile.FileCreationDate = FI.CreationTime;
        list.Add(NewAddedFile);
    }
    Repeater1.DataSource = ????????????;
    Repeater1.DataBind();
}

My repeater in aspx is like below :

<asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
        <asp:Label ID="Label1" runat="server" Text='<%# Eval("FileName") %>'></asp:Label>
        <br />
        <asp:Label ID="Label2" runat="server" Text='<%# Eval("FilePath") %>'></asp:Label>
        <br />
        <asp:Label ID="Label3" runat="server" Text='<%# Eval("FileCreationDate") %>'></asp:Label>
    </ItemTemplate>
</asp:Repeater>

How can I set repeater datasource as that List<> Collection and use it for filling repeated labels?

EDIT :
error appeared after setting Repeater1.DataSource = list;
or
after adding some code in Item_DataBound of that repeater like that answer

DataBinding: 'FileExplorer.Classes.NewAddedFiles' does not contain a property with the name 'FileName'.

Joel
  • 7,401
  • 4
  • 52
  • 58
SilverLight
  • 19,668
  • 65
  • 192
  • 300

6 Answers6

15

Just set your list as the DataSource:

Repeater1.DataSource = list;

EDIT

You don't have actual Properties, you're using Fields. You need to create actual properties in order for the databinding to find them.

So modify your class like:

public class NewAddedFiles
{
    public string FileName { get; set; }
    public string FilePath { get; set; }
    public DateTime FileCreationDate { get; set; }
}
CodingGorilla
  • 19,612
  • 4
  • 45
  • 65
  • Any idea how to implement it in my code please: http://stackoverflow.com/questions/31255119/how-to-populate-two-separate-repeaters-with-distinct-column-name-and-the-value-c Thanks. – SearchForKnowledge Jul 07 '15 at 13:12
4

Um, how about just:

Repeater1.DataSource = list;

That's certainly what I'd expect... have you tried it?

I suspect you're seeing the same values again and again - that's because you're populating your list with multiple references to a single object. You should be creating your NewAddedFile inside your loop:

foreach (FileInfo fi in FileList)
{
    NewAddedFiles file = new NewAddedFiles();
    string relativeFilePath = "~//" + 
        fi.FullName.Replace(Request.ServerVariables["APPL_PHYSICAL_PATH"], "");
    file.FileName = fi.Name;
    file.FilePath = relativeFilePath;
    file.FileCreationDate = fi.CreationTime;
    list.Add(file);
}

Or using LINQ:

List<NewAddedFiles> list = FileList.Select(fi =>
    new NewAddedFiles {
        FileName = fi.Name,
        FilePath = "~//" + fi.FullName
                     .Replace(Request.ServerVariables["APPL_PHYSICAL_PATH"], ""),
        FileCreationPath = fi.CreationTime
    }).ToList();

With respect to the FilePath by the way, I suspect there are better approaches...

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • thanks bro for attention to this thread ... if i could i marked both of your and @just now answers as my answer / – SilverLight Oct 11 '11 at 15:01
2

Repeater1.DataSource = list;

Repeater1.DataBind();

Then handle Item_databound event of repeater

protected void Repeater_ItemDatabound(object s,EventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item 
        || e.Item.ItemType == ListItemType.AlternatingItem) 
    {
        NewAddedFiles currentItem=(NewAddedFiles)e.Item.DataItem;
        //do ur rocessing here
    }
}
Community
  • 1
  • 1
Just Me
  • 244
  • 1
  • 5
1

Simply set the list as the Datasource property: Repeater1.Datasource = list;

Rob
  • 4,927
  • 12
  • 49
  • 54
0

YES...MAKE SURE YOU ADD NewAddedFiles for each iteration. I finally saw the person's comment above but I don't have enough points to give it a one-up.

Mine (that now works for me) in VB (and changed a little bit):

Dim myList As New List(Of NewAddedFiles)()

For Each File In Files

Dim finfo As New System.IO.FileInfo(File)
Dim ThisFileName As String = finfo.Name

'Response.Write(FI.FullName);
'Response.Write("<br />");
Dim AbsoluteFilePath As String = finfo.FullName
Dim RelativeFilePath As String = "~//" + AbsoluteFilePath.Replace(Request.ServerVariables("APPL_PHYSICAL_PATH"), [String].Empty)
Dim NewAddedFile As New NewAddedFiles()
                NewAddedFile.FileName = ThisFileName
                NewAddedFile.FilePath = RelativeFilePath
                NewAddedFile.FileCreationDate = finfo.CreationTime
                myList.Add(NewAddedFile)
Next

            repeater_contactlist.DataSource = myList
            repeater_contactlist.DataBind()

And my aspx looks like this:

        <asp:Repeater runat="server" ID="repeater_contactlist">
            <HeaderTemplate>
                <table cellspacing="0" cellpadding="0" border="0" class="table_volunteers_contacts_dump">
            </HeaderTemplate>
            <ItemTemplate>
                <tr>
                    <td align="left">
                        <tt>
                        <asp:LinkButton ID="lnkbutton_filelink" runat="server" Text='<%# Eval("FileName") %>'></asp:LinkButton></tt>

                    </td>

                </tr>
            </ItemTemplate>
            <FooterTemplate>
                </table>
            </FooterTemplate>
        </asp:Repeater>
Szymon
  • 42,577
  • 16
  • 96
  • 114
user2860427
  • 65
  • 1
  • 2
  • 5
0

you need to create a NewAddedFiles object at each iteration:

private void GetFilesFromDirectory(string PhysicalPath)
{
    DirectoryInfo Dir = new DirectoryInfo(PhysicalPath);
    FileInfo[] FileList = Dir.GetFiles("*.*", SearchOption.AllDirectories);
    List<NewAddedFiles> list = new List<NewAddedFiles>();
      NewAddedFiles NewAddedFileItem = null;
    foreach (FileInfo FI in FileList)
    {
        //you need to create a new object at each iteration
        NewAddedFileItem = new NewAddedFiles();
        //Response.Write(FI.FullName);
        //Response.Write("<br />");
        string AbsoluteFilePath = FI.FullName;
        string RelativeFilePath = "~//" + AbsoluteFilePath.Replace(Request.ServerVariables["APPL_PHYSICAL_PATH"], String.Empty);
        NewAddedFileItem.FileName = FI.Name;
        NewAddedFileItem.FilePath = RelativeFilePath;
        NewAddedFileItem.FileCreationDate = FI.CreationTime;
        list.Add(NewAddedFileItem);
    }
      Repeater1.DataSource = list;
      Repeater1.DataBind();
}
Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70