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

  • Thread starter Thread starter darrel
  • Start date Start date
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
 
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
 
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.
 
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
 
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
 
Back
Top