简体   繁体   中英

FileInfo array won't show up in DataList

My aim is to habe a listing of all wave files in a specific folder in my webapplication project. So I tried this code:

    protected void Page_Load(object sender, EventArgs e)
    {
        string path = Server.MapPath("/alldata/wav/");
        DirectoryInfo di = new DirectoryInfo(path);
        FileInfo[] filelist = di.GetFiles("*.wav", SearchOption.TopDirectoryOnly);
        DataList_filelist.DataSource = filelist;
        DataList_filelist.DataBind();
    }

Markup is:

        <asp:DataList ID="DataList_filelist" runat="server" RepeatColumns="1">
        </asp:DataList>

But my DataList won't be displayed. What did I do wrong?

How about adding an Item Template which will let you display the required thing like

<ItemTemplate>
    <%# Eval("Name") %>
</ItemTemplate>

This should show you the name of the files in the array.

code might show up all the properties if you were to use GridView wherein you can AutoGenerateColumns

Assuming you're populating the datasource (ie you're looking in the right folder, and ASP has read permissions) Just skip the FileInfo[] array, and use the DirectoryInfo.GetFiles as the datasource.

string path = Server.MapPath("/alldata/wav/");
DirectoryInfo di = new DirectoryInfo(path);
DataList_filelist.DataSource = di.GetFiles("*.wav");
DataList_filelist.DataBind();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM