ASP.NET Web Form wants to display file directory

2.4k Views Asked by At

I'm currently attempting to modify this tutorial's code such that I can create an ASP.NET web form which will allow users to view information on and download my selected portfolio works. However, when the page loads, it gets treated as though I want to browse through the part of the server which hosts the files (~/SelectedWorks) and, since my Web.config isn't configured to allow directory browsing at that location - or any location - I get an error.

Below is the ASP.NET code behind the page. The C# code behind the page is, aside from differently-named classes, identical to that of the tutorial's. I've also been using exclusively Visual Studio 2013 Ultimate (and thus its bundled version of IIS Express) to test the page. If someone could help me figure out what's going on, I'd greatly appreciate it!

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="SelectedWorks.aspx.cs" Inherits="ConflictingGenius_ASP.SelectedWorks" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <section>
        <div>
            <hgroup>
                <h2><%: Page.Title %></h2>
            </hgroup>

            <asp:ListView ID="productList" runat="server" 
                DataKeyNames="WorkID" GroupItemCount="4"
                ItemType="ConflictingGenius_ASP.Models.SelectedWork" SelectMethod="GetProducts">
                <EmptyDataTemplate>
                    <table >
                        <tr>
                            <td>No data was returned.</td>
                        </tr>
                    </table>
                </EmptyDataTemplate>
                <EmptyItemTemplate>
                    <td/>
                </EmptyItemTemplate>
                <GroupTemplate>
                    <tr id="itemPlaceholderContainer" runat="server">
                        <td id="itemPlaceholder" runat="server"></td>
                    </tr>
                </GroupTemplate>
                <ItemTemplate>
                    <td runat="server">
                        <table>
                            <tr>
                                <td>
                                    <a href="WorkDetails.aspx?WorkID=<%#:Item.WorkID%>">
                                        <img src="<%#:Item.ImagePath%>"
                                            width="100" height="75" style="border: solid" /></a>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <a href="WorkDetails.aspx?WorkID=<%#:Item.WorkID%>">
                                        <span>
                                            <%#:Item.Title%>
                                        </span>
                                    </a>
                                    <br />
<%--                                    <span>
                                        <a href="<%#:this.ResolveClientUrl(Item.URLPath)%>">Download</a>
                                    </span>--%>
                                    <br />
                                </td>
                            </tr>
                            <tr>
                                <td>&nbsp;</td>
                            </tr>
                        </table>
                        </p>
                    </td>
                </ItemTemplate>
                <LayoutTemplate>
                    <table style="width:100%;">
                        <tbody>
                            <tr>
                                <td>
                                    <table id="groupPlaceholderContainer" runat="server" style="width:100%">
                                        <tr id="groupPlaceholder"></tr>
                                    </table>
                                </td>
                            </tr>
                            <tr>
                                <td></td>
                            </tr>
                            <tr></tr>
                        </tbody>
                    </table>
                </LayoutTemplate>
            </asp:ListView>
        </div>
    </section>
</asp:Content>
1

There are 1 best solutions below

0
On

Add this code to your .cs file.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string[] filePaths = Directory.GetFiles(Server.MapPath("~/Uploads/"));
        List<ListItem> files = new List<ListItem>();
        foreach (string filePath in filePaths)
        {
            files.Add(new ListItem(Path.GetFileName(filePath), filePath));
        }
        productList.DataSource = files;
    }
}

Add this code in your ListView in .aspx file

<ItemTemplete>
    <asp:LinkButton runat="server" PostBackUrl='<%#Eval("Value") %>' ><%#Eval("Text") %></asp:LinkButton>
</ItemTemplate>