• asp.net – extract data from listbox

    Home » Forums » Developers, developers, developers » Web design and development » asp.net – extract data from listbox

    Author
    Topic
    #435079

    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?
    scratch

    Viewing 1 reply thread
    Author
    Replies
    • #1027384

      Peggy – I can’t help, but using the tags instead of the [small] may make your code a bit more readable!

      Code:
      
      dim txtRegion as TextBox
      dim strRegion as String
      ........
      txtRegion = lstSRep.SelectedItem
      strRegion = Left(txtRegion.Text, 3)
      
      • #1027388

        Sorry – Leif – I was just trying to separate the code from my blathering on and on. blush

    • #1027385

      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!

      • #1027389

        Thanks Mark!

        I’ll try this out and let you know.

      • #1027767

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

    Viewing 1 reply thread
    Reply To: asp.net – extract data from listbox

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: