So this is what my code behind looks like:
using System.IO; //very important
namespace secureload
{
public class download : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid downloadlist;
private void Page_Load(object sender, System.EventArgs e)
{
string dir = Request.QueryString["directory"];
if(dir == "")
{
dir = Session["dir"].ToString();
}
//gets the infomation for the DirectoryInfo Object and Create a string Array
DirectoryInfo dirinfo = new DirectoryInfo(HttpContext.Current.Server.MapPath(dir));
//Pass string array with file information to Datagrid's datasource
downloadlist.DataSource = dirinfo.GetFiles("*.*");
//bind datagrid
downloadlist.DataBind();
}
}
And this is what my datagrid code looks like:
<asp:DataGrid id="downloadlist" runat="server" Font-Name="Verdana" AutoGenerateColumns="False" AlternatingItemStyle-BackColor="#eeeeee" HeaderStyle-BackColor="Navy" HeaderStyle-ForeColor="White" HeaderStyle-Font-Size="10pt" HeaderStyle-Font-Bold="True" AllowSorting="True" Font-Names="Verdana">
<AlternatingItemStyle BackColor="#EEEEEE"></AlternatingItemStyle>
<ItemStyle Font-Size="Smaller"></ItemStyle>
<HeaderStyle Font-Size="10pt" Font-Bold="True" ForeColor="White" BackColor="Navy"></HeaderStyle>
<Columns>
<asp:HyperLinkColumn DataNavigateUrlField="Name" DataNavigateUrlFormatString="dltrack.aspx?filename={0:d}" DataTextField="Name" HeaderText="File Name" NavigateUrl="dltrack.aspx?filename="></asp:HyperLinkColumn>
<asp:BoundColumn DataField="LastWriteTime" HeaderText="Last Write Time" DataFormatString="{0:d}">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="Length" HeaderText="File Size" DataFormatString="{0:#,### bytes}">
<ItemStyle HorizontalAlign="Right"></ItemStyle>
</asp:BoundColumn>
</Columns>
</asp:DataGrid>
Now the one thing to now of the datagrid configurations is that I wanted my document urls to be dynamic. I want every download to be logged. So I need that url in the HyperlinkColumn. So in the DataNavigateUrlFormatString is where all of the magic happens. I add the url I want "dltrack.aspx" and my parameters, "?filename=" then to dynamically add the file names to the url you add "{0:d}". This adds the "Name" from the DataTextField field of HyperlinkColumn.
No comments:
Post a Comment