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. How: Get value of dropdownlist in a loop?

How: Get value of dropdownlist in a loop?

Scheduled Pinned Locked Moved ASP.NET
helptutorialquestion
3 Posts 3 Posters 0 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.
  • H Offline
    H Offline
    hounetdev
    wrote on last edited by
    #1

    Ok, I can get the value of a dropdownlist buy doing this: dropdownlist1.selecteditem.value, i have left the default id for all the dropdownlists i have on the page. What I am trying to do is get the value in a loop. here is a simple example of what i am trying to do and what. dim temp as string for i=1 to 10 temp = dropdownlist(i).selecteditem.value next Obviously that didn't work so i tryed temp = dropdownlist"+cstr(i)+".selecteditem.value that also is not working, can anyone help. Thank you, Santana

    A 1 Reply Last reply
    0
    • H hounetdev

      Ok, I can get the value of a dropdownlist buy doing this: dropdownlist1.selecteditem.value, i have left the default id for all the dropdownlists i have on the page. What I am trying to do is get the value in a loop. here is a simple example of what i am trying to do and what. dim temp as string for i=1 to 10 temp = dropdownlist(i).selecteditem.value next Obviously that didn't work so i tryed temp = dropdownlist"+cstr(i)+".selecteditem.value that also is not working, can anyone help. Thank you, Santana

      A Offline
      A Offline
      Angel Reyes
      wrote on last edited by
      #2

      There might be a more efficient way, but you could loop through all of the controls on the page and check the type. If the control's type is dropdown list then retrieve the value Here is some psuedocode (C#) that should give you a basic idea of how to accomplish the task. foreach(Control c in Page.Controls) { if (c.GetType().ToString() == "System.Web.UI.WebControls.DropdownList") { Dropdownlist drp = (DropDownList) c; string retVal = c.selectedItem.value; } } I hope that helps.

      D 1 Reply Last reply
      0
      • A Angel Reyes

        There might be a more efficient way, but you could loop through all of the controls on the page and check the type. If the control's type is dropdown list then retrieve the value Here is some psuedocode (C#) that should give you a basic idea of how to accomplish the task. foreach(Control c in Page.Controls) { if (c.GetType().ToString() == "System.Web.UI.WebControls.DropdownList") { Dropdownlist drp = (DropDownList) c; string retVal = c.selectedItem.value; } } I hope that helps.

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        That's the way to do it. Since there is no such this as a control array in .NET, you have to iterate through the Page's Controls collection, comparing the type of each control to the one your looking for, then you can set a variable to that control, get its Name property and check if its the one you want. The only problem I have with what you wrote is that the type is completely dependent on the string. A better way would be something like this:

        if( c.GetType() == typeof(DropDownList) )

        This way, if the namespace string changes in future versions of the .NET Framework, you won't have to go back and rewrite your code with the new name. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

        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