Can I set a dropDownList to the TEXT rather than index?

D

darrel

I can set the selected item in a dropDownList to this:

DropDownList1.SelectedIndex = int

but is there a way to set it to the text value of the option instead? I've
looked in the documentation, and it appears the only way to set the selected
item is to use SelectedIndex.

-Darrel
 
G

Garett Rogers

This should work for you:
DropDownList1.Items.FindByText("foo").Selected = True

p.s. Make sure you have nothing selected or you will get an error... you
can't select 2 things at once:
DropDownList1.SelectedItem.Selected = False

Garett
http://www.aimx.com
 
J

Joe Fallon

Have you tried:
Me.cboName.SelectedValue = "Sometext"

Set the Datasource and then databind and then try the syntax above.
I think the Item collection is not filled until you call databind.
 
D

darrel

Have you tried:
Me.cboName.SelectedValue = "Sometext"

Hmm...well, I've added this:

CountyData.ReadXml(MapPath("../Judges/JudgeCounty.xml"))
DropDownList1.DataSource = CountyData
DropDownList1.DataValueField = "CountyNumber"
DropDownList1.DataTextField = "CountyName"
DropDownList1.DataBind()
-->DropDownList1.SelectedValue =
(DropDownList1.SelectedItem.text)

But it doesn't work. Thanks, though! I'll keep digging!

-Darrel
 
D

darrel

DropDownList1.Items.FindByText("foo").Selected = True

I added it here:

Try
CountyData.ReadXml(MapPath("../Judges/JudgeCounty.xml"))
DropDownList1.DataSource = CountyData
DropDownList1.DataValueField = "CountyNumber"
DropDownList1.DataTextField = "CountyName"
DropDownList1.DataBind()
-->
DropDownList1.Items.FindByText(DropDownList1.SelectedItem.text).Selected =
True

However, no change. I'm guessing the 'selectedItem' isn't being passed to
the new page.

I did find that if I grab the query string instead, THEN it works...so,
thanks!

-Darrel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top