Results 1 to 6 of 6
  1. #1
    3 Star Lounger
    Join Date
    Jul 2001
    Location
    Minneapolis, Minnesota, USA
    Posts
    299
    Thanks
    0
    Thanked 0 Times in 0 Posts

    asp.net - extract data from listbox

    I almost have my handy-dandy replacement application ready to go, but I ran into a little problem - users like to see dropdown boxes with codes and descriptions (e.g. d dog), and the table to be added/updated just wants the code. Although I've been able to set up a listbox that contains the field:
    "Select salescode + ' ' + Description as DropCode FROM T_Mickey ORDER BY salescode"


    when I try to take the left three for the DropCode and I set it up like:

    dim txtRegion as TextBox
    dim strRegion as String
    ........
    txtRegion = lstSRep.SelectedItem
    strRegion = Left(txtRegion.Text, 3)


    I get....

    "Value of type 'System.Web.UI.WebControls.ListItem' cannot be converted to 'System.Web.UI.WebControls.TextBox'."

    If I set up:

    strRegion = Left(lstSRep.SelectedItem, 3)

    I get:

    Value of type 'System.Web.UI.WebControls.ListItem' cannot be converted to 'String'

    So how do I get the left 3 of DropCode? I think I'm missing something here, right?
    <img src=/S/scratch.gif border=0 alt=scratch width=25 height=29>
    Carpy Diem, it&#39;s .

  2. #2
    Plutonium Lounger Leif's Avatar
    Join Date
    Dec 2000
    Location
    U.K.
    Posts
    14,010
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: asp.net - extract data from listbox

    Peggy - I can't help, but using the <!t>[code]<!/t> tags instead of the <!t>[small]<!/t> may make your code a bit more readable!

    <!t>
    Code:
    <code>
    dim txtRegion as TextBox
    dim strRegion as String
    ........
    txtRegion = lstSRep.SelectedItem
    strRegion = Left(txtRegion.Text, 3)
    </code><!t>

  3. #3
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Indianapolis, Indiana, USA
    Posts
    1,862
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: asp.net - extract data from listbox

    You shouldn't need to use a TextBox object to capture the value from the selected list item.

    Try this:

    Dim mySelectedItem as ListItem
    Dim region as String

    mySelectedItem = lstSRep.SelectedItem
    region = mySelectedItem.Value.Substring(0,3)


    (I'm not sure if you'll want ListItem.Value or ListItem.Text - it just depends on how you've built your list. If one doesn't work, try the other.)

    FYI - the ".NET" way to obtain the first 3 characters is to use Substring() instead of Left().

    Hope this helps!

  4. #4
    3 Star Lounger
    Join Date
    Jul 2001
    Location
    Minneapolis, Minnesota, USA
    Posts
    299
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: asp.net - extract data from listbox

    Sorry - Leif - I was just trying to separate the code from my blathering on and on. <img src=/S/blush.gif border=0 alt=blush width=15 height=15>
    Carpy Diem, it&#39;s .

  5. #5
    3 Star Lounger
    Join Date
    Jul 2001
    Location
    Minneapolis, Minnesota, USA
    Posts
    299
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: asp.net - extract data from listbox

    Thanks Mark!

    I'll try this out and let you know.
    Carpy Diem, it&#39;s .

  6. #6
    3 Star Lounger
    Join Date
    Jul 2001
    Location
    Minneapolis, Minnesota, USA
    Posts
    299
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: asp.net - extract data from listbox

    Mark,

    My .aspx form didn't like the ListItem. I struggled for awhile and then emailed this to a consultant who still owes us some time. Here's what worked:

    dim strRegion as string
    .........
    strRegion = Convert.ToString(lstSRep.SelectedItem.Value)

    I'm busy now getting all the verifications on the fields (please fill in field stuff) and then the application will be just like the end user is expecting. <img src=/S/groovin.gif border=0 alt=groovin width=21 height=21>
    Carpy Diem, it&#39;s .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •