我正在尝试设计一个照片管理器网站应用程序,所以我需要根据文件夹名称显示文件夹中的所有照片,并且只使用1个aspx页面。 我发现许多tutorail可以显示文件夹中的所有照片,但我不知道如何在文件夹中显示照片选择galery,例如:我在主机服务器上有一个文件夹照片,在它有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())但我无法使用&lt;%#bind(&#34;来自aspx页面)调用它名称&#34;)%&gt;,错误属性名称错误! :(
答案 0 :(得分:0)
您的代码中存在两个问题:
您需要创建一个具有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}“)%&gt;'从您的代码中保持简单
ImageUrl='<%# Bind("Name") %>'