2009-08-26 50 views
0

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

UPDATE: 这里是我到目前为止,请让我知道,如果有更好的方法来做到这一点

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

    } 
} 

回答

1

您可以将中继器绑定到任何列表。在你的情况列表的DirectoryInfo的可能是相关的,或者如果您想要的文件和文件夹,某种自定义的对象,它同时拥有:

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

... 

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

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

你把我在正确的轨道上,但我可以做到这一点,而无需创建一个类? 这是我走到这一步, 的 public class FolderClass { private DataSet dsFolder = new DataSet(“ds1”); public FolderClass(s​​tring path){} public static FileInfo [] getFiles() {return new DirectoryInfo(@“E:\ Documents \ Projects \ aaa.com \ albums \ Bridal Bqt”)。GetFiles();} } – PBG 2009-08-28 00:17:17

+0

不是如果你需要处理目录和文件。此外,也许更新你的主要帖子,这有点难以阅读这个小评论部分的代码:)另外,你写的方式,它很好地封装,所以我不会担心它。做得很好。 – 2009-08-28 00:19:20

+0

我编辑了我原来的帖子 – PBG 2009-08-28 12:51:19

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

上面的代码从Web项目的〜/活动文件夹获得所有* .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 
    } 
} 

下面的片段展示了如何从/所有目录〜视频文件夹,也过滤他们只选择含有.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控件的逻辑是相同的(注意我在第一个数据绑定的第二个片段和Filename中调用Foldername数据字段,但可以使用任何名称,需要在标记中设置):

<asp:DropDownList ID="listItems" runat="server" AutoPostBack="True" 
    DataTextField="Foldername" DataValueField="Foldername" 
    OnSelectedIndexChanged="listItems_SelectedIndexChanged" 
    /> 
相关问题