By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,276 Members | 1,311 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,276 IT Pros & Developers. It's quick & easy.

Alter Values in ListBox

P: n/a
A ListBox lists all the files & directories existing in a directory on
the server. If an item in the ListBox happens to be a directory, then
the name of the directory is appended with the text [DIR]. Similarly,
if an item in the ListBox happens to be a file, then the name of the
file is appended with the text [FILE]. Assuming that the ListBox lists
2 directories & 3 files, this is how the ListBox would look like:

MyDir1 [DIR]
MyDir2 [DIR]
File1.aspx [FILE]
File2.aspx [FILE]
File3.aspx [FILE]

Since I want the ListBox to add additional information of each
directory & file, I am adding the items to the ListBox using the Add
method & not using the DataSource property to bind the directories &
files to the ListBox:

Sub Page_Load(.....)
Dim fsi As FileSystemInfo
Dim dInfo As DirectoryInfo

dInfo = New DirectoryInfo(Server.MapPath("Folder1"))

For Each fsi In dInfo.GetFileSystemInfos
If ((fsi.Attributes And FileAttributes.Directory) = 16) Then
lstFD.Items.Add(fsi.Name & " [DIR]")
Else
lstFD.Items.Add(fsi.Name & " [FILE]")
End If
Next
End Sub

When I have s look at the source of the ListBox, I find that the value
assigned to each item in the ListBox is exactly the same as the text of
the item like this:

<select id="lstFD">
<option value="MyDir1 [DIR]">MyDir1 [DIR]</option>
<option value="MyDir2 [DIR]">MyDir2 [DIR]</option>
<option value="File1.aspx [FILE]">File1.aspx [FILE]</option>
<option value="File2.aspx [FILE]">File2.aspx [FILE]</option>
<option value="File3.aspx [FILE]">File3.aspx [FILE]</option>
</select>

As is evident above, the value of each item & its corresponding text is
exactly the same.

I want to remove the text [DIR] & [FILE] appended to each directory &
file respectively ONLY from the value of each item so that the value of
each item gets set to just the directory or the file name i.e. the
source of the ListBox should be

<select id="lstFD">
<option value="MyDir1">MyDir1 [DIR]</option>
<option value="MyDir2">MyDir2 [DIR]</option>
<option value="File1.aspx">File1.aspx [FILE]</option>
<option value="File2.aspx">File2.aspx [FILE]</option>
<option value="File3.aspx">File3.aspx [FILE]</option>
</select>

How do I accomplish this?

I tried adding the following code immediately after For....Next loop
starts (i.e. just before the If condition)

lstFD.DataValueField = fsi.Name

but that doesn't make any difference. The value of each item still
remains exactly the same as its corresponding text.

Jan 8 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
ADDENDUM:
--------

Strangely, if I add the following line:

Response.Write(lstFD.DataValueField & "<br>")

anywhere within the For...Next loop but outside the If...Else condition
like this:

For Each fsi In dInfo.GetFileSystemInfos
lstFD.DataValueField = fsi.Name
If ((fsi.Attributes And FileAttributes.Directory) = 16) Then
lstFD.Items.Add(fsi.Name & " [DIR]")
Else
lstFD.Items.Add(fsi.Name & " [FILE]")
End If
Response.Write(lstFD.DataValueField & "<br>")
Next

then the Response.Write line doesn't spit out the text [DIR] & [FILE]
for each directory & file respectively; it just spits out the directory
name & the file name respectively but the value of each item (when the
source is viewed) still appends the text [DIR] & {FILE] along with the
directory name & file name respectively.

rn**@rediffmail.com wrote:
A ListBox lists all the files & directories existing in a directory on
the server. If an item in the ListBox happens to be a directory, then
the name of the directory is appended with the text [DIR]. Similarly,
if an item in the ListBox happens to be a file, then the name of the
file is appended with the text [FILE]. Assuming that the ListBox lists
2 directories & 3 files, this is how the ListBox would look like:

MyDir1 [DIR]
MyDir2 [DIR]
File1.aspx [FILE]
File2.aspx [FILE]
File3.aspx [FILE]

Since I want the ListBox to add additional information of each
directory & file, I am adding the items to the ListBox using the Add
method & not using the DataSource property to bind the directories &
files to the ListBox:

Sub Page_Load(.....)
Dim fsi As FileSystemInfo
Dim dInfo As DirectoryInfo

dInfo = New DirectoryInfo(Server.MapPath("Folder1"))

For Each fsi In dInfo.GetFileSystemInfos
If ((fsi.Attributes And FileAttributes.Directory) = 16) Then
lstFD.Items.Add(fsi.Name & " [DIR]")
Else
lstFD.Items.Add(fsi.Name & " [FILE]")
End If
Next
End Sub

When I have s look at the source of the ListBox, I find that the value
assigned to each item in the ListBox is exactly the same as the text of
the item like this:

<select id="lstFD">
<option value="MyDir1 [DIR]">MyDir1 [DIR]</option>
<option value="MyDir2 [DIR]">MyDir2 [DIR]</option>
<option value="File1.aspx [FILE]">File1.aspx [FILE]</option>
<option value="File2.aspx [FILE]">File2.aspx [FILE]</option>
<option value="File3.aspx [FILE]">File3.aspx [FILE]</option>
</select>

As is evident above, the value of each item & its corresponding text is
exactly the same.

I want to remove the text [DIR] & [FILE] appended to each directory &
file respectively ONLY from the value of each item so that the value of
each item gets set to just the directory or the file name i.e. the
source of the ListBox should be

<select id="lstFD">
<option value="MyDir1">MyDir1 [DIR]</option>
<option value="MyDir2">MyDir2 [DIR]</option>
<option value="File1.aspx">File1.aspx [FILE]</option>
<option value="File2.aspx">File2.aspx [FILE]</option>
<option value="File3.aspx">File3.aspx [FILE]</option>
</select>

How do I accomplish this?

I tried adding the following code immediately after For....Next loop
starts (i.e. just before the If condition)

lstFD.DataValueField = fsi.Name

but that doesn't make any difference. The value of each item still
remains exactly the same as its corresponding text.
Jan 8 '07 #2

P: n/a
OK....friends I got the solution to specify a different value for each
item in the ListBox. This is how it can be done (for those who might
come across a similar problem in the future):

Dim fsi As FileSystemInfo
Dim dInfo As DirectoryInfo

dInfo = New DirectoryInfo(Server.MapPath("Folder1"))

For Each fsi In dInfo.GetFileSystemInfos
If ((fsi.Attributes And FileAttributes.Directory) = 16) Then
lstFD.Items.Add(New ListItem(fsi.Name & " [DIR]", fsi.Name))
Else
lstFD.Items.Add(New ListItem(fsi.Name & " [FILE]", fsi.Name))
End If
Next
rn**@rediffmail.com wrote:
ADDENDUM:
--------

Strangely, if I add the following line:

Response.Write(lstFD.DataValueField & "<br>")

anywhere within the For...Next loop but outside the If...Else condition
like this:

For Each fsi In dInfo.GetFileSystemInfos
lstFD.DataValueField = fsi.Name
If ((fsi.Attributes And FileAttributes.Directory) = 16) Then
lstFD.Items.Add(fsi.Name & " [DIR]")
Else
lstFD.Items.Add(fsi.Name & " [FILE]")
End If
Response.Write(lstFD.DataValueField & "<br>")
Next

then the Response.Write line doesn't spit out the text [DIR] & [FILE]
for each directory & file respectively; it just spits out the directory
name & the file name respectively but the value of each item (when the
source is viewed) still appends the text [DIR] & {FILE] along with the
directory name & file name respectively.

rn**@rediffmail.com wrote:
A ListBox lists all the files & directories existing in a directory on
the server. If an item in the ListBox happens to be a directory, then
the name of the directory is appended with the text [DIR]. Similarly,
if an item in the ListBox happens to be a file, then the name of the
file is appended with the text [FILE]. Assuming that the ListBox lists
2 directories & 3 files, this is how the ListBox would look like:

MyDir1 [DIR]
MyDir2 [DIR]
File1.aspx [FILE]
File2.aspx [FILE]
File3.aspx [FILE]

Since I want the ListBox to add additional information of each
directory & file, I am adding the items to the ListBox using the Add
method & not using the DataSource property to bind the directories &
files to the ListBox:

Sub Page_Load(.....)
Dim fsi As FileSystemInfo
Dim dInfo As DirectoryInfo

dInfo = New DirectoryInfo(Server.MapPath("Folder1"))

For Each fsi In dInfo.GetFileSystemInfos
If ((fsi.Attributes And FileAttributes.Directory) = 16) Then
lstFD.Items.Add(fsi.Name & " [DIR]")
Else
lstFD.Items.Add(fsi.Name & " [FILE]")
End If
Next
End Sub

When I have s look at the source of the ListBox, I find that the value
assigned to each item in the ListBox is exactly the same as the text of
the item like this:

<select id="lstFD">
<option value="MyDir1 [DIR]">MyDir1 [DIR]</option>
<option value="MyDir2 [DIR]">MyDir2 [DIR]</option>
<option value="File1.aspx [FILE]">File1.aspx [FILE]</option>
<option value="File2.aspx [FILE]">File2.aspx [FILE]</option>
<option value="File3.aspx [FILE]">File3.aspx [FILE]</option>
</select>

As is evident above, the value of each item & its corresponding text is
exactly the same.

I want to remove the text [DIR] & [FILE] appended to each directory &
file respectively ONLY from the value of each item so that the value of
each item gets set to just the directory or the file name i.e. the
source of the ListBox should be

<select id="lstFD">
<option value="MyDir1">MyDir1 [DIR]</option>
<option value="MyDir2">MyDir2 [DIR]</option>
<option value="File1.aspx">File1.aspx [FILE]</option>
<option value="File2.aspx">File2.aspx [FILE]</option>
<option value="File3.aspx">File3.aspx [FILE]</option>
</select>

How do I accomplish this?

I tried adding the following code immediately after For....Next loop
starts (i.e. just before the If condition)

lstFD.DataValueField = fsi.Name

but that doesn't make any difference. The value of each item still
remains exactly the same as its corresponding text.
Jan 8 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.