将转发器绑定到文件和/或文件夹列表

时间:2009-08-26 00:44:33

标签: asp.net objectdatasource

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

更新: 以下是我到目前为止的情况,如果有更好的方法,请告诉我

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

   }
}

2 个答案:

答案 0 :(得分:1)

您可以将转发器绑定到任何列表。在您的情况下,列表DirectoryInfo可能是相关的,或者如果您想要文件和文件夹,某种自定义对象同时包含两者:

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

...

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

repFoo.DataSource = fsos;
repFoo.DataBind();

答案 1 :(得分:0)

您可以使用.NET匿名类型和LINQ,如下面的代码,来自ClipFlair(http://clipflair.codeplex.com)图库元数据输入页面(假设使用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
  }
}

以上代码段从您的网站项目的〜/ activity文件夹中获取所有* .clipflair文件

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

以下代码段显示了如何使用多个过滤器(基于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"
    />