0

我想创建一个非常简单的图片库。我试图弄清楚如何将中继器绑定到某种自定义对象,该对象将返回文件和/或文件夹列表。有人可以指出我正确的方向吗?

更新:这是我到目前为止所拥有的,如果有更好的方法,请告诉我

ListView 显示我的文件夹

<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 显示我的缩略图

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

这是 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();

   }
}
4

2 回答 2

1

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();
于 2009-08-26T00:59:47.163 回答
0

您可以使用 .NET 匿名类型和 LINQ,例如 ClipFlair ( http://clipflair.codeplex.com ) Gallery 元数据输入页面中的以下代码(假设 using System.Linq 子句):

private string path = HttpContext.Current.Server.MapPath("~/activity");

protected void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack) //only at page 1st load
  {
    listItems.DataSource =
      Directory.EnumerateFiles(path, "*.clipflair")
               .Select(f => new { Filename=Path.GetFileName(f) });
    listItems.DataBind(); //must call this
  }
}

上面的代码片段从您的 Web 项目的 ~/activity 文件夹中获取所有 *.clipflair 文件

更新:使用 EnumerateFiles(自 .NET 4.0 起可用)而不是 GetFiles,因为这对 LINQ 查询更有效。GetFiles 将在 LINQ 有机会过滤它之前返回内存中的整个文件名数组。

以下代码段显示了如何使用多个过滤器(基于 Can you call Directory.GetFiles() with multiple filters?中的回答),其中 GetFiles/EnumerateFiles 自身不支持:

private string path = HttpContext.Current.Server.MapPath("~/image");
private string filter = "*.png|*.jpg";

protected void Page_Load(object sender, EventArgs e)
{
  _listItems = listItems; 

  if (!IsPostBack)
  {
    listItems.DataSource =
      filter.Split('|').SelectMany(
        oneFilter => Directory.EnumerateFiles(path, oneFilter)
                     .Select(f => new { Filename = Path.GetFileName(f) })
      );

    listItems.DataBind(); //must call this

    if (Request.QueryString["item"] != null)
      listItems.SelectedValue = Request.QueryString["item"];
                          //must do after listItems.DataBind
  }
}

下面的代码片段显示了如何从 /~video 文件夹中获取所有目录并过滤它们以仅选择包含与目录同名的 .ism 文件(平滑流内容)的目录(例如 someVideo/someVideo.ism)

private string path = HttpContext.Current.Server.MapPath("~/video");

protected void Page_Load(object sender, EventArgs e)
{ 
  if (!IsPostBack) //only at page 1st load
  {
    listItems.DataSource = 
      Directory.GetDirectories(path)
        .Where(f => (Directory.EnumerateFiles(f, Path.GetFileName(f) + ".ism").Count() != 0))
        .Select(f => new { Foldername = Path.GetFileName(f) });
    //when having a full path to a directory don't use Path.GetDirectoryName (gives parent directory),
    //use Path.GetFileName instead to extract the name of the directory

    listItems.DataBind(); //must call this
  }
}

上面的示例来自 DropDownList,但它与任何支持数据绑定的 ASP.net 控件的逻辑相同(请注意,我在第二个片段中调用 Foldername 数据字段,在第一个片段中调用 Filename,但可以使用任何名称,需要在标记中设置):

  <asp:DropDownList ID="listItems" runat="server" AutoPostBack="True" 
    DataTextField="Foldername" DataValueField="Foldername" 
    OnSelectedIndexChanged="listItems_SelectedIndexChanged"
    />
于 2013-07-14T23:42:14.320 回答