473,412 Members | 4,127 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

fill dropdownlist with filename

Hi,
I wan to fill a dropdownlist with the filenames in a certain directory, i
use the following statement for retrieving the files:

DirectoryInfo di = new
DirectoryInfo("C:\\Inetpub\\wwwroot\\ASPNET\\Docum ents");
FileInfo[] rgFiles = di.GetFiles("*.doc");
foreach(FileInfo fi in rgFiles)
{
return (fi.Name);
}

but how can I connect this with the dropdownlist, i need to see there the
file names.

thanks in advance,

Remco
--
thanks,

Remco Ploeg
Nov 19 '05 #1
4 3823
Remco,

Get rid of the foreach statement. Make rgFiles protected. In the .aspx file
write:

<asp:ddl id=ddlFiles runat=server DataSource=<%# rgFiles %>
DataTextField=Name />

Eliyahu

"Remco Ploeg" <Re********@discussions.microsoft.com> wrote in message
news:7A**********************************@microsof t.com...
Hi,
I wan to fill a dropdownlist with the filenames in a certain directory, i
use the following statement for retrieving the files:

DirectoryInfo di = new
DirectoryInfo("C:\\Inetpub\\wwwroot\\ASPNET\\Docum ents");
FileInfo[] rgFiles = di.GetFiles("*.doc");
foreach(FileInfo fi in rgFiles)
{
return (fi.Name);
}

but how can I connect this with the dropdownlist, i need to see there the
file names.

thanks in advance,

Remco
--
thanks,

Remco Ploeg

Nov 19 '05 #2
Hi,

The way to do this is to set the DataSource property of the
DropDownList with the list of files. Once you do that, then you want to
tell the DropDownList what properties of the FileInfo class it should
use as the Text and Value attributes of the DropDownList. Here is the
code that you need:

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
// set the path to your directory here
DirectoryInfo di = new DirectoryInfo(@"C:\");
FileInfo[] fis = di.GetFiles();
DropDownList1.DataSource=fis;
DropDownList1.DataTextField="Name";
DropDownList1.DataValueField="Name";
DropDownList1.DataBind();
}
}
Thanks,

Sayed Y. Hashimi

http://www.sayedhashimi.com
Shameless Book Plug: Service-Oriented Smart Clients with .NET 2.0
http://www.amazon.com/exec/obidos/tg...glance&s=books

Nov 19 '05 #3
Hi,

i created the following code and that's working:

private void loadFilesDataSet ()
{
string [] files = Directory.GetFiles
("C:\\inetpub\\wwwroot\\aspnet\\Documents\\");
object [] values;
System.Data.DataTable filestable = new DataTable ("Files");
filestable.Columns.Add (new DataColumn ("Filename", typeof
(string)));

for (int counter = 0; counter < files.Length; counter++)
{
if (files [counter].EndsWith (".doc") || files
[counter].EndsWith (".doc"))
{
values = new object [] {Path.GetFileName (files
[counter])};
filestable.Rows.Add (values);
}
}

DataSet test = new DataSet ("Filenames");
test.Tables.Add (filestable);
DataView abc = test.Tables [0].DefaultView;
DropDownList4.DataSource = abc;
DropDownList4.DataTextField =
abc.Table.Columns["Filename"].ColumnName.ToString();
DropDownList4.DataBind();

return;
}
--
thanks,

Remco Ploeg
"ha**********@gmail.com" wrote:
Hi,

The way to do this is to set the DataSource property of the
DropDownList with the list of files. Once you do that, then you want to
tell the DropDownList what properties of the FileInfo class it should
use as the Text and Value attributes of the DropDownList. Here is the
code that you need:

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
// set the path to your directory here
DirectoryInfo di = new DirectoryInfo(@"C:\");
FileInfo[] fis = di.GetFiles();
DropDownList1.DataSource=fis;
DropDownList1.DataTextField="Name";
DropDownList1.DataValueField="Name";
DropDownList1.DataBind();
}
}
Thanks,

Sayed Y. Hashimi

http://www.sayedhashimi.com
Shameless Book Plug: Service-Oriented Smart Clients with .NET 2.0
http://www.amazon.com/exec/obidos/tg...glance&s=books

Nov 19 '05 #4
Hi,
One idea would be to build a HashTable or a SortedList object, inside your
for-each loop, then DataBind() the object to your control in the normal
fashion.

so it would look something like this;
#######################
DirectoryInfo di = new
DirectoryInfo("C:\\Inetpub\\wwwroot\\ASPNET\\Docum ents");
var oSList as New SortedList
FileInfo[] rgFiles = di.GetFiles("*.doc");
foreach(FileInfo fi in rgFiles)
{
oSList.Add(fi.Name, fi.Name);
}
oSList.TrimToSize()
return oSList;

#######################

and then just DataBind() it to your page control as normal;

#######################
myDropDownList.DataSource=oSList
myDropDownList.DataValueField="Key"
myDropDownList.DataTextField="Value"
myDropDownList.DataBind()
#######################

peace
--
Thanks,
$.Trundle
MCSE

"Remco Ploeg" wrote:
Hi,
I wan to fill a dropdownlist with the filenames in a certain directory, i
use the following statement for retrieving the files:

DirectoryInfo di = new
DirectoryInfo("C:\\Inetpub\\wwwroot\\ASPNET\\Docum ents");
FileInfo[] rgFiles = di.GetFiles("*.doc");
foreach(FileInfo fi in rgFiles)
{
return (fi.Name);
}

but how can I connect this with the dropdownlist, i need to see there the
file names.

thanks in advance,

Remco
--
thanks,

Remco Ploeg

Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Maurice Mertens | last post by:
Hi all, In VB.NET you can set the DropDownStyle for a combobox to 'DropDown' or 'DropDownList'. When you set it to DropDownList, it supports auto-fill. But when you set it to 'DropDown', the...
4
by: theo | last post by:
Program flow...load file,then extract the xml text tags from the file,then the number of Xml tags retrieved from the file determines the number of dropdownlist controls instanciated in the...
3
by: tshad | last post by:
I am trying to fill my DropDownList (actually a few of them) with data from my sql table. I am calling Stored procedures for all of them. I would like to use the first line as a place holder...
4
by: VB Programmer | last post by:
I am trying to use a dropdownlist to fill in the ORDER BY of a SqlDataSource. Here's part of my HTML... <asp:SqlDataSource ID="VideoList" runat="server" ConnectionString="<%$...
1
by: Bob | last post by:
I've made an upload function saving to a specific folder and dropdownlist reading and listing files from the folder. When I upload, the list is rendered a second time in the dropdownlist with the...
2
by: mosscliffe | last post by:
I am trying to use IndexChanged of DropDownList to control the next part of my site. The problem I have is that the 'indexchanged' is not getting called if I select the first entry on first...
3
by: phanimadhav | last post by:
hi this is sudheer, i am new one of this ASP.NET .i have one problem,i am using gridview control i know how to place the dropdownlistbox in gridview control.in my gridveiw control have contain...
2
by: Fabio Mastria | last post by:
Hi all! In a my simple project I use callback to fill a dropdownlist with xml data returned by a web service, based on a value which is input via another dropdownlist. NOTE: I can't use...
1
by: Probi | last post by:
HI, I have a DropDownList populated from database I use the following code OracleDataAdapter ad2 = new OracleDataAdapter(cmd2, connection); DataSet ds2 = new DataSet(); ad2.Fill(ds2); ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.