Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Populating Dropdown Lists

Populating Dropdown Lists

Scheduled Pinned Locked Moved ASP.NET
databasecsharpasp-netcom
6 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Megan Forbes
    wrote on last edited by
    #1

    We've recently moved over from ASP 3 for our front end to ASP.NET (using VB.NET sadly). In the past we'd populate our dropdown lists for searches so that the value and the display showed different fields from our database. For example, to allow the user to select a firm in a search, the value might hold the firm id, but the display would be the firm name. Now I'm trying to do the same thing with ASP.NET. Using cmbFirms.Items.Add(.Item("firm_name")) for example (where .Item("firm_name")) is in a with block where a recordset is being read out) results in both the value and the display obviously containing the firm's name. Is there a way to give the value of the option selected a different value from the display? I've been trying things like : cmbFirms.SelectedIndex.Equals(.Item("firm_id")) cmbFirms.SelectedValue.Equals(.Item("firm_name")) and cmbFirms.Items.IndexOf(.Item("firm_id")) cmbFirms.DataValueField = .Item("firm_id") in desperation to set the index to the firm id while keeping the display friendly as the list of firm names, but to no avail. Any clues? Thanks :)


    Look at the world about you and trust to your own convictions. - Ansel Adams
    Meg's World - Blog Photography

    C T 2 Replies Last reply
    0
    • M Megan Forbes

      We've recently moved over from ASP 3 for our front end to ASP.NET (using VB.NET sadly). In the past we'd populate our dropdown lists for searches so that the value and the display showed different fields from our database. For example, to allow the user to select a firm in a search, the value might hold the firm id, but the display would be the firm name. Now I'm trying to do the same thing with ASP.NET. Using cmbFirms.Items.Add(.Item("firm_name")) for example (where .Item("firm_name")) is in a with block where a recordset is being read out) results in both the value and the display obviously containing the firm's name. Is there a way to give the value of the option selected a different value from the display? I've been trying things like : cmbFirms.SelectedIndex.Equals(.Item("firm_id")) cmbFirms.SelectedValue.Equals(.Item("firm_name")) and cmbFirms.Items.IndexOf(.Item("firm_id")) cmbFirms.DataValueField = .Item("firm_id") in desperation to set the index to the firm id while keeping the display friendly as the list of firm names, but to no avail. Any clues? Thanks :)


      Look at the world about you and trust to your own convictions. - Ansel Adams
      Meg's World - Blog Photography

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      Yes, you can give the item a different value to the display. However, I'm not too familiar with VB.NET syntax: What does the .Item(...) mean. What is the implicit thing that would go infront of the dot? Anyway, You do something like the following (sorry it's in C# syntax)

      cmbFirms.Items.Add(new ListItem(strDisplayText, strInternalValue));

      Does this help?


      "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!

      M 2 Replies Last reply
      0
      • C Colin Angus Mackay

        Yes, you can give the item a different value to the display. However, I'm not too familiar with VB.NET syntax: What does the .Item(...) mean. What is the implicit thing that would go infront of the dot? Anyway, You do something like the following (sorry it's in C# syntax)

        cmbFirms.Items.Add(new ListItem(strDisplayText, strInternalValue));

        Does this help?


        "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!

        M Offline
        M Offline
        Megan Forbes
        wrote on last edited by
        #3

        Thanks Colin, I'm off to try and hack that into VB.NET now ;) A pity about the C# thing - I was outvoted in the department with VB.NET winning out :(( Colin Angus Mackay wrote: What does the .Item(...) mean sorry - I wasn't very clear in my first post - this is already within a With block reading a recordset - so that's just a field coming back from SQL :)


        Look at the world about you and trust to your own convictions. - Ansel Adams
        Meg's World - Blog Photography

        1 Reply Last reply
        0
        • C Colin Angus Mackay

          Yes, you can give the item a different value to the display. However, I'm not too familiar with VB.NET syntax: What does the .Item(...) mean. What is the implicit thing that would go infront of the dot? Anyway, You do something like the following (sorry it's in C# syntax)

          cmbFirms.Items.Add(new ListItem(strDisplayText, strInternalValue));

          Does this help?


          "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!

          M Offline
          M Offline
          Megan Forbes
          wrote on last edited by
          #4

          :cool: Thanks! This works beautifully: With rsRisk If .HasRows Then 'if the database has returned something While .Read cmbFirms.Items.Add(New ListItem(.Item("firm_name"), .Item("firm_id"))) End While Else bla bla bla Onto the next challenge... :-D


          Look at the world about you and trust to your own convictions. - Ansel Adams
          Meg's World - Blog Photography

          C 1 Reply Last reply
          0
          • M Megan Forbes

            We've recently moved over from ASP 3 for our front end to ASP.NET (using VB.NET sadly). In the past we'd populate our dropdown lists for searches so that the value and the display showed different fields from our database. For example, to allow the user to select a firm in a search, the value might hold the firm id, but the display would be the firm name. Now I'm trying to do the same thing with ASP.NET. Using cmbFirms.Items.Add(.Item("firm_name")) for example (where .Item("firm_name")) is in a with block where a recordset is being read out) results in both the value and the display obviously containing the firm's name. Is there a way to give the value of the option selected a different value from the display? I've been trying things like : cmbFirms.SelectedIndex.Equals(.Item("firm_id")) cmbFirms.SelectedValue.Equals(.Item("firm_name")) and cmbFirms.Items.IndexOf(.Item("firm_id")) cmbFirms.DataValueField = .Item("firm_id") in desperation to set the index to the firm id while keeping the display friendly as the list of firm names, but to no avail. Any clues? Thanks :)


            Look at the world about you and trust to your own convictions. - Ansel Adams
            Meg's World - Blog Photography

            T Offline
            T Offline
            Thea Burger
            wrote on last edited by
            #5

            Just a suggestion, you probably have a reason for using the recordset. But if you used a dataset with your dropdown list, you could do this: ddlSelected.DataSource = ds.Tables(0) ddlSelected.DataTextField = ds.Tables(0).Columns("au_lname").ColumnName.ToString() ddlSelected.DataValueField = ds.Tables(0).Columns("au_id").ColumnName.ToString() ddlSelected.DataBind() :-) Thea

            1 Reply Last reply
            0
            • M Megan Forbes

              :cool: Thanks! This works beautifully: With rsRisk If .HasRows Then 'if the database has returned something While .Read cmbFirms.Items.Add(New ListItem(.Item("firm_name"), .Item("firm_id"))) End While Else bla bla bla Onto the next challenge... :-D


              Look at the world about you and trust to your own convictions. - Ansel Adams
              Meg's World - Blog Photography

              C Offline
              C Offline
              Colin Angus Mackay
              wrote on last edited by
              #6

              Glad to help.


              "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups