Thursday, January 15, 2004

So last night I stayed at work a little later, and I got to work on my C# project. It was one of those times when things just flowed. What I needed to write was something that displayed files in a directory and I need to be able to specify the directory on the fly. So first thing I did a google search. So I come across this article from 4GuysFromRolla.Com on "Displaying the Files in a Directory using a DataGrid". This is almost what I need, however the examples are in VB.NET not C#, not really that big of a deal, just translate some syntax. So in the end what I wrote was a Datagrid that displays all files in a given directory, and that directory can change on the fly within the scope of my project.

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. DataNavigateUrlFormatString="dltrack.aspx?filename={0:d}" This seems to run pretty fast, and took lets time to code up then creating your own dynamic tables. In classic ASP, I would touch datagrids with a ten foot pole, but in ASP.NET they do actually make your life easier!

No comments: