user724198
user724198

Reputation:

ASP.NET ListBox: Display both text & value?

I have a listbox on my aspx page.

I'd like it to display both the text and associated values (not the index). So if I were to add items like this

Dim this As ListItem = New ListItem("Horse", "2")
Dim that As ListItem = New ListItem("Dog", "3")

me.ListBox.Items.Add(this)
me.ListBox.Items.Add(that)

my listbox would look something like this (please excuse my list of artistic talent):

--------------
|2 Horse     |
|3 Dog       |
--------------

Is there any way to do this?

Thanks,

Jason

Upvotes: 1

Views: 1492

Answers (2)

Tom Studee
Tom Studee

Reputation: 10452

Sure, why not just combine the text and the value into a variable.

Dim nextItem as ListItem = New ListItem(String.Concat("2", " ", "Horse"), "2")

ListBox.Items.Add(nextItem)

Upvotes: 1

Jakob Gade
Jakob Gade

Reputation: 12427

Add the ID as part of the item text?

Dim this As ListItem = New ListItem("2 Horse", "2")
Dim that As ListItem = New ListItem("3 Dog", "3")

Upvotes: 2

Related Questions