CodeSeeker

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

My Links

News

Archives

Blogs

Thursday, September 10, 2009

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()

posted @ Thursday, September 10, 2009 11:38 PM | Feedback (3) |

Powered by: