2009-08-26 48 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" 
    /> 
相關問題