PDA

Click to See Complete Forum and Search --> : RESOLVED!! Adding Text and Value to DropDown


ARPRINCE
Apr 23rd, 2004, 03:32 PM
How do you add text and Value to a dropdown programatically?

Right now I use the binding method with no problems.


DropDownList1.DataTextField = "Name"
DropDownList1.DataValueField = "CustomerID"
DropDownList1.DataSource = SQLQuery()
DropDownList1.DataBind()


However I have a need to do it manually on some of my dropdown web controls.

I can add items into the collection but the TEXT and VALUE are both the same when it should have been (ex Finance, 001).


With DropDownList2
.Items.Add("Finance")
.Items.Add("Design")
.Items.Add("Management")
End With



**** DATA ****

Dept Code
------- ------
FINANCE 001
DESIGN 002
MANAGEMENT 003

TIF

axion_sa
Apr 24th, 2004, 12:12 PM
The ListItemCollection.Add function is overloaded - you're currently using Combo.Items.Add(string). The second option is Combo.Items.Add(ListItem) - which would have the "ability" to do what you want:

With DropDownList2
' ListItem([Text], [Value]) constructor.
.Items.Add(New ListItem("Finance", "001"))
End With

ARPRINCE
Apr 26th, 2004, 08:47 AM
This tip did the trick. Thanks