开发者

How can I use a List<> Collection as a Repeater Datasource in ASP.NET with C#

开发者 https://www.devze.com 2023-04-12 01:05 出处:网络
I have a list collection like below : using System; using System.Collections.Generic; using System.Linq;

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&开发者_StackOverflow中文版gt;
        <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'.


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


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


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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号