Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datagrid file download links broken

Status
Not open for further replies.

markdmac

MIS
Dec 20, 2003
12,340
US
I am using the following code to display the contents of a directory on the server. I want the datagrid to allow download of each of the files. The datagrid is successfully populating the table however the hyperlinks go to the root of the site instead of the directory of the server.mappath and I don't know why. Can anyone please provide some guidance as to what I am doing wrong?

Code:
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>


<html>
<head>

<script language="VB" runat="server">
  Sub Page_Load(sender as Object, e as EventArgs)
        Dim dirInfo As New DirectoryInfo(Server.MapPath("Uploads"))
    
        articleList.DataSource = dirInfo.GetFiles("*.*")
        articleList.DataBind()
  End Sub
</script>


</head>
<body>

<div Align="center">
    <img src="Images/SN_LOGO_3.gif" />
</div>
<div id="navigation">
    <center>
    <hr /><hr /><br />
    <a href="FileDownload.aspx"><img src="Images/download.jpg" border="0" /></a>
    <a href="FileUpload.aspx"><img src="Images/upload.jpg" border="0" /></a>
    </center>
    <br /><hr />
</div>
    <hr />
<div id="downloadarea">
    <div id="dlHeader" style="font-size: xx-large">Available Downloads</div>
<asp:DataGrid runat="server" id="articleList" Font-Name="Verdana"
    AutoGenerateColumns="False" AlternatingItemStyle-BackColor="#0099ff"
    HeaderStyle-BackColor="#0099ff" HeaderStyle-ForeColor="White"
    HeaderStyle-Font-Size="15pt" HeaderStyle-Font-Bold="True">
  <Columns>
    <asp:HyperLinkColumn DataNavigateUrlField="Name" DataTextField="Name" 
           HeaderText="File Name" />
    <asp:BoundColumn DataField="LastWriteTime" HeaderText="File Date"
        ItemStyle-HorizontalAlign="Center" DataFormatString="{0:d}" />
    <asp:BoundColumn DataField="Length" HeaderText="File Size"
		ItemStyle-HorizontalAlign="Right" 
		DataFormatString="{0:#,### bytes}" />
  </Columns>
</asp:DataGrid>  

</div>
&nbsp;

</body>
</html>

I hope that helps.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
First, don't mix your code with the markup, this is classic ASP coding. Use the code-behind file of the page.
Second, the hyperlinkcolumn you have, as a DataNavigateURLField property set to "Name". I am not sure what that is. If it is just the name of the file, it won't work. You will have to find a property with the full path of the file.
 
I found a better example here:

I hope that helps.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top