我有一个列表集合,如下所示:
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(); }
我在aspx中的转发器如下所示:
<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>
如何将转发器数据源设置为List<>收集并使用它填充重复的标签?
编辑:
设置Repeater1.DataSource = list后出现错误
要么
在该中继器的Item_DataBound中添加一些代码,就像该答案一样
DataBinding: ‘FileExplorer.Classes.NewAddedFiles’ does not contain a
property with the name ‘FileName’.
解决方法
只需将您的列表设置为DataSource:
Repeater1.DataSource = list;
编辑
您没有实际的属性,您正在使用字段.您需要创建实际属性才能使数据绑定找到它们.
所以修改你的类,如:
public class NewAddedFiles { public string FileName { get; set; } public string FilePath { get; set; } public DateTime FileCreationDate { get; set; } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。