Search code examples
c#asp.netasprepeater

Create asp:Repeater with links from list of files in folder


I have an application that creates PDFs and stores them on a file within the website. I need to loop through each of those files and find documents with a specific name on it. I'm able to create that list without any issues, but getting stumped with the best method to create my links

This is what I have so far:

string rootFolderPath = Server.MapPath("~/Forms/Offers");
string listOfBidSheets = @"*" + prospect.LastName.Trim() + "_" + prospect.FirstName.Trim() + "*";
string[] fileList = System.IO.Directory.GetFiles(rootFolderPath, listOfBidSheets);

if (fileList.Length > 0)
{  
    rptPDFLinks.DataSource = fileList;
    rptPDFLinks.DataBind();
}

The part that is stumping me is what to put on the front-end of the code:

<li><%# DataBinder.Eval(Container.DataItem, "Item") %></li>

Normally when I bind to a repeater the Container.DataItems are easy because they are column names.

Any thoughts on where to take this? I'm open to different solutions all together.


Solution

  • The DataBinder class is a fine way to render a property of the DataItem with some optional formatting. In your case the DataItem is just a string so you don't want to use the DataBinder. You can directly use the DataItem as such:

    <li><%# Container.DataItem %></li> 
    

    This code won't give you a link though. You want to do something like this:

    <li><a href="/Forms/Offers/<%# Container.DataItem %>"><%# Container.DataItem %></a></li>
    

    You also need to strip out the path from the file names. Here's an example using some LINQ (you need to add 'using System.Linq' at the top of your file:

    rptPDFLinks.DataSource = fileList.Select(x => System.IO.Path.GetFileName(x));
    rptPDFLinks.DataBind();