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

发布于 2024-12-09 12:39:41 字数 2038 浏览 2 评论 0 原文

我有一个如下所示的列表集合:

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>

如何将转发器数据源设置为该列表<>收集并用它来填充重复的标签?

编辑:
设置Repeater1.DataSource = list;后出现错误

在该中继器的 Item_DataBound 中添加一些代码后,就像这个答案一样

数据绑定:“FileExplorer.Classes.NewAddedFiles”不包含 名为“FileName”的属性。

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'.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

月朦胧 2024-12-16 12:39:41

只需将您的列表设置为数据源

Repeater1.DataSource = list;

编辑

您没有实际的属性,您正在使用字段。您需要创建实际属性以便数据绑定能够找到它们。

所以修改你的类:

public class NewAddedFiles
{
    public string FileName { get; set; }
    public string FilePath { get; set; }
    public DateTime FileCreationDate { get; set; }
}

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; }
}
你的背包 2024-12-16 12:39:41

嗯,只是这样怎么样:

Repeater1.DataSource = list;

这当然是我所期望的......你尝试过吗?

我怀疑您一次又一次地看到相同的值 - 这是因为您使用对单个对象的多个引用填充列表。您应该在循环内创建 NewAddedFile

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);
}

或者使用 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();

顺便说一下,关于 FilePath,我怀疑有更好的方法...

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

浅暮の光 2024-12-16 12:39:41

Repeater1.DataSource = 列表;

Repeater1.DataBind();

然后处理repeater的Item_databound事件

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
    }
}

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
    }
}
御弟哥哥 2024-12-16 12:39:41

只需将列表设置为 Datasource 属性:Repeater1.Datasource = list;

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

还给你自由 2024-12-16 12:39:41

您需要在每次迭代时创建一个 NewAddedFiles 对象:

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();
}

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();
}
允世 2024-12-16 12:39:41

是的...请确保为每次迭代添加 NewAddedFiles。我终于看到了上面那个人的评论,但我没有足够的积分来给它一分。

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()

我的 aspx 看起来像这样:

        <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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文