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

リピーターは任意のリストにバインドできます。あなたの場合、DirectoryInfoのリストが関連しているかもしれません。あるいは、ファイルとフォルダが必要な場合は、両方を保持するある種のカスタムオブジェクトです。

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

ClipFlair(http://clipflair.codeplex.com)ギャラリーメタデータ入力ページ(System.Linq句を使用していると想定)から、以下のコードのように.NET匿名型と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ファイルを取得します

更新:GetFilesの代わりにEnumerateFiles(.NET 4.0以降で使用可能)を使用します。これは、LINQクエリでより効率的であるためです。GetFilesは、LINQがファイル名をフィルタリングする前に、メモリ内のファイル名の配列全体を返します。

次のスニペットは、複数のフィルターを使用する方法を示しています(複数のフィルターを使用して Directory.GetFiles()を呼び出すことができますか?)。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フォルダーからすべてのディレクトリを取得する方法を示しています。また、ディレクトリと同じ名前(someVideo / someVideo.ismなど)の.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をデータフィールドと呼び、最初のスニペットでFilenameと呼んでいますが、任意の名前を使用できます。マークアップで設定する必要があります):

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