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

时间:2011-10-11 14:30:33

标签: c# asp.net collections repeater datasource

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

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&lt;&gt;收集并用它来填充重复的标签?

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

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

  

DataBinding:'FileExplorer.Classes.NewAddedFiles'不包含   名为'FileName'的属性。

6 个答案:

答案 0 :(得分:14)

只需将list设为DataSource

即可
Repeater1.DataSource = list;

修改

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

所以修改你的类如:

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

答案 1 :(得分:4)

嗯,嗯,怎么样:

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,我怀疑有更好的方法......

答案 2 :(得分:2)

  

Repeater1.DataSource = 列表;

     

Repeater1.DataBind();

然后处理转发器的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
    }
}

答案 3 :(得分:1)

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

答案 4 :(得分:0)

您需要在每次迭代时创建一个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();
}

答案 5 :(得分:0)

是...确保您为每次迭代添加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>