How can I use a List<> Collection as a Repeater Datasource in ASP.NET with C#
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; }}
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...
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 }}