【问题标题】:Display photos in dynamic folder asp.net在动态文件夹 asp.net 中显示照片
【发布时间】:2014-10-06 12:40:51
【问题描述】:

我正在尝试设计一个照片管理器网站应用程序,因此我需要根据文件夹名称显示文件夹中的所有照片,并且只使用 1 个 aspx 页面。 我发现许多教程可以显示文件夹中的所有照片,但我不知道如何在选择画廊时显示文件夹中的照片,例如:我在主机服务器上有一个文件夹照片,其中有 2 个子文件夹: 动物和花。当我点击动物文件夹时,所有的动物照片都会显示在网页上等等当点击花卉文件夹时,鲜花照片就会显示出来。

我的代码在这里: ASPX 页面:

<asp:DataList ID="DataList1" runat="server" RepeatColumns="5" BackColor="White"
            BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3"   ForeColor="Black"
             Width="100%">
           <FooterStyle BackColor="#CCCCCC" />
                       <SelectedItemStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
                        <HeaderTemplate>
                                   <span class="style2">Image Gallary</span>
            </HeaderTemplate>
                <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
                      <ItemTemplate>
                      <asp:ImageButton Width="105px" ID="Image1" runat="server"  BorderStyle="Solid" ImageUrl='<%# Bind("Name", "~/[foldername]/{0}") %>'
                      Height="94px"  />
                          <br />
                          <asp:LinkButton ID="HyperLink1" Text='<%# Bind("Name") %>'  CommandArgument='<%# Bind("Name") %>'  runat="server" />
                      </ItemTemplate>
                          <FooterStyle BackColor="White" ForeColor="#333333" />
                          <ItemStyle BorderColor="Silver" BorderStyle="Dotted" BorderWidth="1px" HorizontalAlign="Center"
                              VerticalAlign="Bottom" BackColor="White" ForeColor="#333333" />
</asp:DataList>

代码隐藏

private void ListImages()
{
    DirectoryInfo dir = new DirectoryInfo(MapPath("~/images")); // it's will be animal if i click on animal and flower when i click on flower.
    FileInfo[] file = dir.GetFiles();
    ArrayList list = new ArrayList();
    foreach (FileInfo file2 in file)
    {
        if (file2.Extension == ".jpg" || file2.Extension == ".jpeg" || file2.Extension == ".gif" || file2.Extension == ".png")
        {
            list.Add(file2);
        }
    }
    DataList1.DataSource = list;
    DataList1.DataBind();
}

我尝试将完整路径添加到列表中:list.add(dir.tostring()+file2.tostring()) 但我无法使用 ,错误是错误的属性名! :(

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    你的代码有两个问题:

    您需要创建一个具有Name 属性的属性类。像这样的:

    public class ImageTest
    {
      public string Name {get;set;}
    }
    

    然后在你的 bindlist 方法中:

    private void ListImages()
            {
                DirectoryInfo dir = new DirectoryInfo(MapPath("~/images/Animal")); // it's will be animal if i click on animal and flower when i click on flower.
                FileInfo[] file = dir.GetFiles();
                // ArrayList list = new ArrayList();
                List<ImageTest> list = new List<ImageTest>();
                foreach (FileInfo file2 in file)
                {
                    if (file2.Extension == ".jpg" || file2.Extension == ".jpeg" || file2.Extension == ".gif" || file2.Extension == ".png")
                    {
                        // list.Add(file2);
                        // list.Add(dir.ToString() + file2.ToString());
                        list.Add(new test()
                                     {
                                         Name = "http://localhost:58822/Images/Animal/" + file2.ToString() // where localhost path would be your site url
                                     });
                    }
                }
                DataList1.DataSource = list;
                DataList1.DataBind();
            }
    

    您还需要从代码中删除 ~/[foldername]/{0}") %>' 并保持简单

     ImageUrl='<%# Bind("Name") %>'
    

    【讨论】:

    • 谢谢!!!我找到了我的解决方案,在数据绑定的项目上自定义绑定 image.imageurl! :D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-25
    • 2012-01-24
    相关资源
    最近更新 更多