views:

333

answers:

1

I would like to create a very simple image gallery. I am trying to figure out how to bind a Repeater to some kind of a custom object that would return back a list of files and/or folders. Can somebody point me in the right direction?

UPDATE: Here's what i have so far, please let me know if there's a better way to do this

ListView to display my folders

<asp:ListView ID="lvAlbums" runat="server" DataSourceID="odsDirectories">
    <asp:ObjectDataSource ID="odsDirectories" runat="server" SelectMethod="getDirectories" TypeName="FolderClass">
       <SelectParameters>
          <asp:QueryStringParameter DefaultValue="" Name="album" QueryStringField="album" Type="String" />
       </SelectParameters>
    </asp:ObjectDataSource>

ListView to display my thumbnails

<asp:ListView ID="lvThumbs" runat="server" DataSourceID="odsFiles">
<asp:ObjectDataSource ID="odsFiles" runat="server" SelectMethod="getFiles" TypeName="FolderClass">
   <SelectParameters>
      <asp:QueryStringParameter Type="String" DefaultValue="" Name="album" QueryStringField="album" />
   </SelectParameters>
</asp:ObjectDataSource>

And here's FolderClass

public class FolderClass
{
   private DataSet dsFolder = new DataSet("ds1");

   public static FileInfo[] getFiles(string album)
   {
      return new DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath("/albums/" + album)).GetFiles();

   }
   public static DirectoryInfo[] getDirectories(string album)
   {
      return new DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath("/albums/" + album)).GetDirectories()
                .Where(subDir => (subDir.Name) != "thumbs").ToArray();

   }
}
A: 

You can bind a repeater to any list. In your case a list DirectoryInfo's may be relevant, or if you want files AND folders, some sort of custom object that holds both:

class FileSystemObject
{
    public bool IsDirectory;
    public string Name;
}

...

List<FileSystemObject> fsos = ...; // populate this in some fashion

repFoo.DataSource = fsos;
repFoo.DataBind();
Noon Silk
You put me on the right track, but can i can do this without creating a class?Here's what i got so far <asp:Repeater ID="rpThumbs" runat="server" DataSourceID="ObjectDataSource1"><asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="getFiles" TypeName="FloralExtraDesign.FolderClass"> public class FolderClass { private DataSet dsFolder = new DataSet("ds1"); public FolderClass(string path){} public static FileInfo[] getFiles() {return new DirectoryInfo(@"E:\Documents\Projects\aaa.com\albums\Bridal Bqt").GetFiles();} }
Pasha
Not if you need to handle Directories AND files. Also, maybe update your main post, it's a bit hard to read code in this little comment section :) Also, the way you have written it, it is quite nicely encapsulated, so I wouldn't worry about it. Nicely done.
Noon Silk
I edited my original post
Pasha