Wednesday, September 17, 2008

Displaying the Files in a Directory using a DataGrid

In his article from http://aspnet.4guysfromrolla.com/articles/052803-1.aspx
Scott Mitchell describe the method.
Here is the c# version

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace WebApplication2
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var rootFolder = @"C:\Documents and Settings\All Users\Documents\Mes images\Échantillons d'images";

var selectedImages = from file in Directory.GetFiles(rootFolder,"*.jpg")
select new {
Name= new FileInfo(file).Name,

};

imageList.DataSource = selectedImages;
imageList.DataBind();
}

}
}

Here is the aspx page


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:DataGrid runat="server" ID="imageList" Font-Name="Verdana" 

                      AutoGenerateColumns="False"

                      AlternatingItemStyle-BackColor="#eeeeee" 

                      HeaderStyle-BackColor="Navy" 

                      HeaderStyle-ForeColor="White"

                      HeaderStyle-Font-Size="15pt" 

                      HeaderStyle-Font-Bold="True">

            <Columns>

               <asp:HyperLinkColumn DataNavigateUrlField="Name" 

                                    DataTextField="Name" HeaderText="Nom du Fichier"  

                                    ItemStyle-ForeColor="DarkOrange" 

                                    ItemStyle-BackColor="#FF0066" />

            </Columns>

        </asp:DataGrid>

    </div>

    </form>

</body> 

</html>




No comments:

Post a Comment