加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

如何在ASP.NET中使用列表<>集合作为Repeater数据源与C#

发布时间:2020-12-15 22:21:25 所属栏目:asp.Net 来源:网络整理
导读:我有一个列表集合,如下所示: 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 FileC
我有一个列表集合,如下所示:
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; }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读