Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Working with enums and dropdowns

Status
Not open for further replies.

rrhandle

Programmer
Dec 26, 2001
193
0
0
US
The application has two enum named AnimalEnums
DOG=1000
CAT=1001

The page has a dropdown name ddlAnimal

How can I populate the dropdown list in Page_Load with the two enum values?

Also, the type of animal has already been select on the previous page, so I would like that animal to appear already selected in the drop-down.

Thanks


 
See the following thread on how to pass variables.


As for filling the drop down list with the value of the enum just do...

Code:
 Me.DropDownList1.Items.Add(New ListItem("Dog", animalEnums.Dog))
        Me.DropDownList1.Items.Add(New ListItem("Cat", animalEnums.Cat))

then to select the one from the other page you would say...

 Me.DropDownList1.SelectedIndex = Me.DropDownList1.Items.IndexOf(Me.DropDownList1.Items.FindByValue(ValuePassedOver))

I'm assuming that you are passing over the value instead of the text, however if you pass the word "Dog" (for instance then you would used "FindByText" instead.
 
Thanks, jshurst, but that requires you to manually enter "DOG" and "CAT". The code would need to be updated if new animals are added.

I found this to work:

Code:
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Fill Dropdown with Enums and select value being passed in
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim oPet As objPets.AnimalType
Dim names As String() = System.Enum.GetNames(GetType(objPets.AnimalEnum))
For i As Integer = 0 To names.Length - 1
  Me.ddlPets.Items.Add(names(i))
  If names(i) = oPet.AnimalEnum.GetName(GetType(objPets.AnimalEnum), [value_passed_in]) Then
    Me.ddlCageLine.SelectedIndex = i + 1
  End If
Next

 
BTW, thanks for reminding me about the FindValue method. Completely forgot about it.

--Rob

 
Cool, glad you found a solution. I didn't know that you could do enum.getnames so you taught me something.

Glad I could help, even if just a little.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top