Alter Values in ListBox

Discussion in 'ASP .Net' started by rn5a@rediffmail.com, Jan 8, 2007.

  1. Guest

    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, 2007
    #1
    1. Advertising

  2. Guest

    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.

    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, 2007
    #2
    1. Advertising

  3. Guest

    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


    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.
    >
    > 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, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kersh

    Alter Rendered HTML for page

    Kersh, Jul 24, 2003, in forum: ASP .Net
    Replies:
    6
    Views:
    503
    John Saunders
    Aug 6, 2003
  2. eRic
    Replies:
    2
    Views:
    387
  3. Wee Bubba
    Replies:
    2
    Views:
    392
    Wee Bubba
    May 17, 2004
  4. -berlin.de
    Replies:
    0
    Views:
    143
    -berlin.de
    Jun 30, 2007
  5. Jeremy

    Alter Form values before submission

    Jeremy, Nov 17, 2005, in forum: Javascript
    Replies:
    1
    Views:
    82
Loading...

Share This Page