views:

671

answers:

6

I am using a codebehind page in ASP.NET to perform a SQL query. The query is loaded into a string, the connection is established (To Oracle), and we get it started by having the connection perform .ExecuteReader into a OleDBDataReader (We'll call it DataRead). I'll try to hammer out an example below. (Consider Drop as an ASP DropDownList control)

Dim LookFor as String = "Fuzzy Bunnies"

While DataRead.Read
    If LookFor = DataRead.Item("Kinds of Bunnies") Then
        'Meets special critera, do secondary function'

         Drop.Items.Add(DataRead.Item("Subgroup of Bunnies"))
         ...
    End if
    ...
End While

This is the only way I know of doing a dynamic add to a DropDownList. However, each item in a DropDownList has a .text property and a .value property. How can we define the .value as being different from the .text in code?

A: 

Pardon my possibly faulty VB

Dim item as New ListItem()
item.Value = "foo"
item.Text = "bar"

Drop.Items.Add(item)

You can also use the ListItem constructor (e.g. new ListItem("text", "value"))

John Sheehan
+2  A: 

Add should have an overload that accepts a ListItem object. Using that, you can usually do something like this:


Drop.Items.Add(New ListItem("Text", "Value"))
AaronSieb
You said pretty much exactly the same thing as me except about 30 seconds earlier. Upvoted :)
Grank
+6  A: 

The Add function can take a ListItem, so you can do

Dim li as new ListItem(DataRead.Item("Subgroup of Bunnies"), "myValue")
Drop.Items.Add(li)
bdukes
A: 

you'd select a second column into your datareader (such as an IDENTITY field) and then assign do your Item generation like this:

Dim item as new listitem
item.text = DataRead.Item("SubGroup Of Bunnies")
item.value = DataRead.Item("ID")
Drop.Items.Add(item)

You may also want to look into the DATABIND functionality, and filtering out "FUZZY BUNNIES" in the SQL statement itself.

Stephen Wrighton
+2  A: 

If I understand the question, Items.Add has an overload that takes a ListItem, so you could create a new ListItem object in that line:

Drop.Items.Add(new ListItem("text", "value"))
Grank
Several people beat me to this answer
Grank
Your answer is the way I would do it ... up vote!
mattruma
AaronSieb said the exact same thing while I was typing mine, so he really should get the upvote... I'll upvote him myself :)
Grank
A: 

Actually, with a little help here at the office, I think we may have found our own solution.

If we create a new ListItem, one of the constructors allows for you to define text AND value, and if we then feed that ListItem in, we get the desired result of differeint text AND value.

EDIT: Six answers between post and this? Wow. Thanks. This site is awesome. Thanks for the help!

MadMAxJr