Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

select item from dropdownlist where value = key 1

Status
Not open for further replies.

cjburkha

Programmer
Jul 30, 2004
76
0
0
US
Hi all, I have a dropdownlist that I populate like this
Code:
                eventDL.DataSource = sqlSelect("Select * from [event]")
                eventDL.DataTextField = "eventName"
                eventDL.DataValueField = "pk_Event"

My problem is I want to set the selected index based on the string value of the dropdownlist, but I am using value to store the index.

I made a loop
Code:
                eventDL.DataSource = sqlSelect("Select * from [event]")
                eventDL.DataTextField = "eventName"
                eventDL.DataValueField = "pk_Event"

But I think there should be a more efficent way. Any ideas?

Sorry if this is the most repeated question, but I see a lot of people using value to store the display string, but I am using value to store the primary key of my DB so I don't have to lookup again.

Thanks again

CJB
 
It is unclear to me what exactly you want to do. YOu have a dropdownlist, Value field is the index, good, the text field is something else, good.. Now, please describe what you want to do once it it is populated.
 
Jeez I'm a dolt.

I want to make the following code more efficient:
Code:
                i = 0
                For Each Item As ListItem In eventDL.Items
                    If Item.Text = "asdf" Then
                        eventDL.SelectedIndex = i
                    End If
                    i += 1
                Next

I would like to set the selected text to something I want
 
Try this, I use it all the time:
Code:
eventDL.Items.FindByText("asdf").Selected = True
 
Thanks, thats exactly what I was looking for.

I knew there was a built in method for what I was trying to do, I just couldn't find it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top