Alter Values in ListBox

  • Thread starter Thread starter rn5a
  • Start date Start date
R

rn5a

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.
 
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.

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.
 
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


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.

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.
 
Back
Top