2009-08-26 5 views
0

非常に簡単なイメージギャラリーを作成したいと思います。 Repeaterをファイルやフォルダのリストを返すカスタムオブジェクトにバインドする方法を解明しようとしています。誰かが私を正しい方向に向けることができますか?リピーターをファイルやフォルダのリストにバインドする

UPDATE: は、ここで私がこれまで持っているもの、私のサムネイルを表示するために私のフォルダ

<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

あなたは正しい軌道に乗っていますが、私はクラスを作成せずにこれを行うことはできますか? ここで私は、これまでに得たもの の パブリッククラスFolderClass { プライベートDataSet dsFolder =新しいDataSet( "ds1"); パブリックFolderClass {} public static FileInfo [] getFiles() {新しいディレクトリ情報を返します(@ "E:\ Documents \ Projects \ aaa.com \ albums \ Bridal Bqt")。GetFiles();} } – PBG

+0

ディレクトリとファイルを処理する必要はありません。また、メインのポストを更新してください。この小さなコメントセクションでコードを読むのは少し難しいです:)また、あなたが書いた方法では、それはかなりうまくカプセル化されているので、心配しません。うまくいった。 –

+0

オリジナルの投稿を編集しました – PBG

0

をあなたは(ClipFlairから下記のコードのように、.NET匿名型とLINQを使用することができます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クエリを使用すると効率的です。 GetFilesは、LINQがフィルタリングする機会を得る前に、メモリ内のファイル名の配列全体を返します。

次のスニペットは、GetFiles/EnumerateFilesが自分自身をサポートしていません。これは、(Can you call Directory.GetFiles() with multiple filters?の回答に基づいて)複数のフィルタを使用する方法を示しています。

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コントロールと同じロジックです(2番目のスニペットではFoldername、2番目のスニペットではFilenameと呼びますが、任意の名前を使用でき、マークアップで設定する必要があります)。

<asp:DropDownList ID="listItems" runat="server" AutoPostBack="True" 
    DataTextField="Foldername" DataValueField="Foldername" 
    OnSelectedIndexChanged="listItems_SelectedIndexChanged" 
    /> 
関連する問題