CodeSeeker

Just another .NET developer trying to do the right thing
posts - 14, comments - 17, trackbacks - 0

My Links

News

Archives

Blogs

Get Both Text and Value in a DropDownList Bound to an ArrayList

If you just need a quick and dirty DropDownList of, let's say, Month values, you can simply build an ArrayList to populate it with, then bind the DropDownList to the ArrayList. This is simple and examples are easy to find on the 'net, but here's the trick: when creating ListItems for your ArrayList, give the ListItems both a Text and Value. Then set the DataTextField of the DropDownList to "Text", and the DataValueField to "Value". Then both the Text and Value properties from the ArrayList are populated into the appropriate fields of the DropDownList!

Here's the VB code:

Dim months As New ArrayList
months.Add(New ListItem("January", "1"))
months.Add(New ListItem("February", "2"))
months.Add(New ListItem("March", "3"))
months.Add(New ListItem("April", "4"))
months.Add(New ListItem("May", "5"))
months.Add(New ListItem("June", "6"))
months.Add(New ListItem("July", "7"))
months.Add(New ListItem("August", "8"))
months.Add(New ListItem("September", "9"))
months.Add(New ListItem("October", "10"))
months.Add(New ListItem("November", "11"))
months.Add(New ListItem("December", "12"))

ddlMonth.DataSource = months
ddlMonth.DataTextField = "Text"
ddlMonth.DataValueField = "Value"
ddlMonth.DataBind()

Print | posted on Thursday, September 10, 2009 11:38 PM |

Feedback

Gravatar

# re: Get Both Text and Value in a DropDownList Bound to an ArrayList

Thanks for the sharing!
9/11/2009 1:48 AM | MCSE Training
Gravatar

# re: Get Both Text and Value in a DropDownList Bound to an ArrayList

thank you for you useful info.
9/11/2009 7:39 AM | cheap jordan shoes
Gravatar

# re: Get Both Text and Value in a DropDownList Bound to an ArrayList

Thanks for Sharing this!
9/19/2009 9:08 PM | Mitchell Lee
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 

Powered by: